如何在其他功能中调用在setup功能中创建的用户

时间:2017-04-26 12:25:49

标签: php unit-testing laravel-5 phpunit laravel-5.1

这是我的完整代码,我将其写入控制器的测试功能

       <?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\Concerns\MakesHttpRequests;    

class RouteForecastTest extends TestCase
{   

    protected $password = 'rightpassword';

     public function setUp()
     {
        parent::setUp();
        $this->createApplication();

        $this->user = factory(App\User::class)->create([
                'email' => 'testuser@email.com',
                'password' => bcrypt($this->password),
                'type' => 'admin'
                ]);
    }

    /** @test */
    public function createTest()
    {   
        $this->actingAs($user);
        $this->visit('/client/ocp/profile/247')
             ->click('New ')
             ->seePageIs('/client/ocp/profile/247/create')
             ->see('name');
    }
    public function tearDown(){
        parent::tearDown();
        $this->user->delete();
    }
}

我尝试使用setup和teardown方法编写此代码并使用phpunit运行测试我收到此错误

ErrorException: Undefined variable: user

我该如何解决此错误?

0 个答案:

没有答案