setUp will be run for every test in the class, but that should be fine I would assume? If you are using the RefreshDatabase trait, laravel will automatically roll back data after each test. Can you explain why your current code is a problem?
Aug 5, 2022
7
Level 1
How to Run setup once, and all methods inside Test Class can access the variables?
So I have this test where I like to setup it once and setup the global variables that way I can use the same data on all methods inside my test class.
I am creating something like this, in a certain directly tests/Unit/Subscription/WithCard.php
class WithCard extends TestCase
{
protected $patient;
protected $plan;
protected $patientSubscription;
// i like to run this once, and use the variables patient, plan, and patientSubscription from all the methods.
public function setup(): void {
$this->patient = PatientFactory::new()->create();
$this->plan = SubscriptionPlan::new()->create();
$this->patientSubscription = new Subscription($this->patient, $this->plan)->subscribe();
}
public function testCheckIfSubscriptionWasSuccessfullyCreated()
{
$this->assertNotNull($this->patientSubscription);
}
public function testCheckIfSuccessfullyPay() {
$isPaid = Subscription::isPatientPaid($this->patient, $this->plan);
$this->assertTrue($isPaid);
}
// a lot of code
}
I tried using setUpBeforeClass but Im not successfull either.
Please or to participate in this conversation.