即使在调用withAnyArgs()时,laravel phpunit模拟了需要数组的方法?

时间:2013-12-13 16:19:08

标签: unit-testing laravel phpunit laravel-4 mockery

下面是我的控制器类和相关方法以及我的测试类和相关测试。当模拟方法$ this-> user-> update()运行时,我收到此错误

Time: 1.08 seconds, Memory: 31.00Mb

There was 1 error:

1) UserControllerTest::testUpdateSuccess
ErrorException: Argument 1 passed to Mockery_818785360_User::update() must be of
 the type array, string given, called in C:\Users\Mark\Dropbox\www\trainercompar
e\app\controllers\UsersController.php on line 134 and defined

除非另有定义,否则模拟方法不应该采用我发送的任何参数?即使我将模拟对象方法更改为包含(m :: type'string'),我也会得到相同的错误。最终,第一个参数将是一个字符串,第二个参数将是一个数组,但我甚至无法达到目标。

UserController.php     

class UsersController extends BaseController {

    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        $user = $this->user->find($id);

        if ($id == Auth::user()->id) {

            $input = Input::all();

            $validation = $this->user->validate($input);

            if ($validation->passes()) {

                $this->user->update(); 

            }


        }
        else {
            echo 'update failed';
        }
    }


}

UserControllerTest.php

<?php
    use Mockery as m;
    use Way\Tests\Factory;
class UserControllerTest extends TestCase {

    public function setUp()
    {
        parent::setUp();
        $this->mock = m::mock('Basemodel', 'User');
    }

    public function tearDown()
    {
        m::close();
    }


    public function testUpdateSuccess()
    {
        $input = ['email' => 'john@doe.com',
                'password' => 'johndoepw',
                'firstName' => 'john',
                'secondName' => 'doe',
                'dob' => '1985-12-12',
                'height' => '187.96',
                'gender' => 'm',
                'phone' => '0877777777',
                'postcode' => '01',
                'addOne' => '29 black market',
                'addTwo' => 'malahide',
                'townCity' => 'Dublin',
                'county' => 'Dublin',
                'country' => 'Ireland'
                ];

        $userid = m::mock('userid');
        $userid->id = '1';
        $view = m::mock('viewret');
        $valmock = m::mock(['passes' => true]);

        Auth::shouldReceive('user')
            ->times(1)
            ->andReturn($userid);

        $this->mock
            ->shouldReceive('find')
            ->times(1)
            ->andReturn($userid->id);

        //Input::shouldReceive('all')->times(1);

        $this->mock
            ->shouldReceive('validate')
            ->times(1)
            ->andReturn($valmock);

        $this->mock
            ->shouldReceive('update')
            ->withAnyArgs()
            ->times(1);

        $this->app->instance('User', $this->mock);
        $this->call('Put', 'users/1', $input);
        $this->assertResponseOk();
    }

}

1 个答案:

答案 0 :(得分:1)

(mockery instance)->shouldReceive仅在运行函数后验证参数。所以,在您的控制器代码中:

if ($validation->passes()) {
  $this->user->update(); 
}

您实际上并没有将任何内容传递给更新功能。我假设你想传入$ input。所以,它应该是$this->user->update($input);

我假设您的用户实例有一个方法update,需要传递数组。