

$this->call('GET', '/order') and not directly calling user model's getOrders method.īut it's on new topic which I created on stackoverflow before. AspectMock is an advanced mocking framework which allows you to replace any methods of any class in a test. It's beause of different instances of Auth. Why is the find method failing If I use a different method (e.g. That means if I run the test, Auth::id() returns NULL in scopeForUser(). Looking ad xdebug trace, I see that it calls the mock::find method, then immediately calls splautoloadcall to get the BadMethodCallException class. Problem is that I access Auth::id() within scopeForUser method which is not set in time of running the test (I updated post), even if I log in user within testGetUsersOrders() method. Not only that but it create a very readable form of documentation for your entire system. Its far more readable, if you return to this test at a later date you, or another developer instantly know what its testing. For detailed information on a particular feature, select one of the following: Mock objects. I opt for the following "PHPSpec like" naming convention: /** */ That is why it is recommended to do integration tests when testing Laravel models.Īnother tip? When writing test methods in place of naming you functions: public function testGetUsersOrders() Then all you need to do is call Dumm圜onnection::mock () from anywhere in your test class. You could easily modify this to make runQuer圜allback () return some fake content after running some checks against the query and bindings parameters. Therefore its not really possible to seperate the two when testing. It involves swapping out the Connection class for a dummy-one. Mocking eloquent models is difficult and leaves your tests very brittle. In active record ORMs your models or entities are coupled to the ORM itself, to be honest your models ARE your ORM. The way you are testing this, in the controller constructor is passed an instance of the real Eloquent model, not the mock. The reason that you really should do this is a naunce of the active record pattern that Eloquent uses. Integrations tests are run within an 'integrated system', that means with a database and real world data. You would be better suited to do integration tests.

If you ever refactor the getOrders() method the tests will break, as your testing specific API calls within this method, not simply testing the return values or functionality of the method. Mocking eloquent models is difficult and leaves your tests very brittle.
