简体   繁体   中英

Laravel 5.1 Mockery expectation ignored while testing controller

I have a Controller depending on a UserManager. This is the controller constructor:

public function __construct(UserManager $manager) {
    $this->manager = $manager;
}

This is the test code.

public function test_something() {
    $this->withoutMiddleware();

    // Setup Input.
    $user = ['email' => 'foo@bar.baz', 'password' => 'pass', 'accessLevel' => 'admin'];

    // Setup expectations.
    $mock = \Mockery::mock("Users\UserManager")->shouldReceive('foo');

    // Bind to container... not sure whether this is needed.
    $this->app->instance("Users\UserManager", $mock);

    // Call action.
    $this->call('POST', 'api/v1/temp/users', ['user' => $user]);
}

I set the expectation on the foo method, which doesn't exists and therefore is not invoked anywhere, however my test won't fail.

Why?

You need to specify how many times the foo method should be called:

->shouldReceive('foo')->once();

Make sure that you also have a tearDown method where you reset Mockery or it won't work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM