hamidelgendy's avatar

Maintaining a single user record on multiple test methods?

Hello Everyone,

I have an Eloquent model named "Customer" also I have a CustomerTest class that extends TestCase. In the CustomerTest, I have more than 4 test methods and a setUp() method that basically creates a Customer and pass it along for each test, The issue is, Each test creates a Customer, I do not want that. I need to create a single Customer to be used for all the tests I have.

Think of it this way, Instead of having the SetUp Method run for each test method, I want the SetUp Method to run once and keep track of the customer created object. Is there a work around this issue I am having ?

0 likes
2 replies
ifpingram's avatar
Level 4

@hamidelgendy You need to approach the testing in a slightly different way. The tests should all run in isolation from each other and not pass an object around which has its properties changed as it moves through the system. Instead you should look at seeding each individual test with the Customer object with its relevant state for that particular part of the system that it is in. The first part of this is done by the setUp() already; what you now need to do is to bring the Customer object to the desired state at the beginning of each test, then run the test and check that the desired state of the object / collaborators have been set correctly by the code under test.

Good luck!

1 like
bugsysha's avatar

Keep track of the customer created object?

Maybe you should use?

use Illuminate\Foundation\Testing\DatabaseTransactions;
1 like

Please or to participate in this conversation.