php - Stuck on mocking 3 classes for 1 unit test -
in authentication.php have following function create test for:
function showmodal() { $registration = new registration; $registration->needshowmodal = store::isusersupported() ? false : true; }
in test case far, have mocked store:
function testshowmodal() { $mockstore = $this->getmockclass(‘store', array('isusersupported')); $mockstore::staticexpects($this->any()) ->method('isusersupported') ->will($this->returnvalue(true)); }
i’m guessing next need create mockregistration , pass , mockstore authentication controller, along lines of:
$controller = new authentication($mockstore,$mockregistration);
however, when create authentication keeps complaining expects request , response object first 2 params.. maybe that’s not right way.
how can best write test isusersupported == true , isusersupported == false? i’m dealing 3 different classes in test that’s i’m getting stuck.
as method written, not able pass mocks replace objects.
you calling new registration
create instance of actual class rather using mock.
the static call store::isusersupported()
call actual method. staticexpects
works when testing class being called statically (you have static method calls different method in same class). mock not work.
to make testable method should become this:
function showmodal($registration, $store) { $registration->needshowmodal = store->isusersupported() ? false : true; }
now able create mocks dependencies , pass them method.
though seem testing action on mvc framework controller. in experience, testing controllers tests end being more "integration" rather "unit". rather trying control these dependencies, try control data source being used , make sure response comes set appropriately proper request. gives benefit of knowing other classes (which have been tested well) have assumptions right being passed them.
without knowing more framework or rest of code, best able do.
Comments
Post a Comment