1

下面是我的控制器类和相关方法以及我的测试类和相关测试。当模拟方法 $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

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

用户控制器.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';
        }
    }


}

用户控制器测试.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();
    }

}
4

1 回答 1

1

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

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

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

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

于 2014-02-28T20:04:13.400 回答