【问题标题】:Laravel mocking route parametersLaravel 模拟路由参数
【发布时间】:2021-11-02 14:34:05
【问题描述】:

我目前正在对我的一些表单验证进行单元测试,并且需要模拟一个路由参数,以便它可以通过测试。我已经为请求设置了我认为正确的期望,但我没有正确地做某事。

Rule::unique('users')->ignore($this->route('user')->id)

这是我试图通过的测试模拟。我尝试做的所有事情都是因为路由上的用户属性为空。

$userMock = $this->mock(User::class)->expects()->set('id', 1);

$requestMock = $this->mock(Request::class)
        ->makePartial()
        ->shouldReceive('route')
        ->set('user', $user)
        ->once()
        ->andReturn(\Mockery::self());

$this->mock(Rule::class, function ($mock) use ($userMock, $requestMock) {
    $mock->expects()->unique('user')->andReturns(\Mockery::self());
    $mock->expects()->ignore($requestMock)->andReturns(\Mockery::self());
});

【问题讨论】:

    标签: php laravel testing phpunit mockery


    【解决方案1】:

    您没有按照应有的方式进行测试:

    • 当你测试与 Laravel 核心相关的东西时,你Feature test。
    • 如果您想测试自己的class 或Job 或Command,请Unit test。
    • 当你想测试外部 API 时(即使是 localhost 但它是其他系统),你可以使用 Integration tests。

    所以,我会写一个 功能测试 向你展示你应该做的事情,所以请记住,我将编写可能你设置不同或没有设置的假路线和工厂甚至设置(我将使用Laravel 8 和PHP 8):

    namespace Tests\Feature;
    
    use App\Models\User;
    use Illuminate\Foundation\Testing\RefreshDatabase;
    use Tests\TestCase;
    
    class ShowTest extends TestCase
    {
        use RefreshDatabase;
    
        public function test_error_is_thrown_when_user_is_not_unique()
        {
            /**
             * Create a fake user so we have an
             * existing user in the DB because
             * that is what we want to test
             *
             * This should end up as last_name = Doe
             */
            User::factory()->create([
                'last_name' => $lastName = 'Doe'
            ]);
    
            /**
             * This is going to be our
             * logged in user and we will
             * send this data.
             *
             * Fake last_name so we do not
             * end up with Doe when faker runs.
             * 
             * @var User $ownUser
             */
            $ownUser = User::factory()->create(['last_name' => 'Lee']);
    
            /**
             * We will simulate sending an update
             * so we can change the last_name of
             * our logged in user, but there is
             * another user with the same last name
             */
            $response = $this->actingAs($ownUser)
                ->put("/fake/route/{$ownUser->id}", ['last_name' => $lastName]);
    
            /**
             * If you don't want to assert what error
             * is comming back, just
             * write ...Errors('last_name') but I
             * recommend checking what is giving back
             */
            $response->assertSessionHasErrors(['last_name' => 'Literal expected error string.']);
        }
    }
    

    我希望你能理解我在这里测试的内容。如果您还有其他问题,请提出。

    另外,如果你能分享你的真实代码,我可以和你一起编写测试,并尝试让你的代码 100% 测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-05
      • 2021-02-04
      • 2017-01-15
      • 2018-04-05
      • 2017-01-13
      • 2013-11-24
      • 2017-10-25
      • 1970-01-01
      相关资源
      最近更新 更多