First of all, I'm aware the docs states:
Note: You should not mock the Request facade. Instead, pass the input you desire into the HTTP helper methods such as call and post when running your test.
But those kind of tests are more like integration or functional since even though you're testing a controller (the SUT), you're not decoupling it from it's dependencies (Request and others, more on this later).
So what I'm doing, in order to do the correct TDD loop, is mocking the Repository, Response and Request (which I have problems with).
My test look like this:
public function test__it_shows_a_list_of_categories() {
$categories = [];
$this->repositoryMock->shouldReceive('getAll')
->withNoArgs()
->once()
->andReturn($categories);
Response::shouldReceive('view')
->once()
->with('categories.admin.index')
->andReturnSelf();
Response::shouldReceive('with')
->once()
->with('categories', $categories)
->andReturnSelf();
$this->sut->index();
// Assertions as mock expectations
}
This works perfectly fine, and they follow the Arrange, Act, Assert style.
The problem is with Request, like the following:
public function test__it_stores_a_category() {
Redirect::shouldReceive('route')
->once()
->with('categories.admin.index')
->andReturnSelf();
Request::shouldReceive('only')
->once()
->with('name')
->andReturn(['name' => 'foo']);
$this->repositoryMock->shouldReceive('create')
->once()
->with(['name' => 'foo']);
// Laravel facades wont expose Mockery#getMock() so this is a hackz
// in order to pass mocked dependency to the controller's method
$this->sut->store(Request::getFacadeRoot());
// Assertions as mock expectations
}
As you can see I have mocked Request::only('name') call. But when I run $ phpunit I get the following error:
BadMethodCallException: Method Mockery_3_Illuminate_Http_Request::setUserResolver() does not exist on this mock object
Since I'm not calling directly setUserResolver() from my controller, that means it is called directly by the implementation of Request. But why? I mocked the method call, it shouldn't be calling any dependency.
What am I doing wrong here, why am I getting this error message?
PS: As a bonus, am I looking it the wrong way by forcing TDD with Unit Tests on Laravel framework, as it seems the documentation is oriented to integration testing by coupling interaction between dependencies and SUT with $this->call()?
Aucun commentaire:
Enregistrer un commentaire