You create mocks from within testcases using the getMock() function, which is defined in PHPUnit_Framework_TestCase like so: getMock($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE) You can also tell PHPUnit to override certain functions to return set values / assert that they're called in a specific way. It creates a class that extends the one you want to mock. PHPUnit has a built in mock object creator which can generate mocks for classes (inc. You can create a mock database connection which responds in the way the model expects, but doesn't actually connect to a physical database. Say for example you're testing a model - you want to make sure that the model is running the correct query, but you don't want it to run on a real database server. Sometimes when writing tests you need to test something that depends on an object being in a certain state. You can’t really do that in PHP, but you can do it through a Javascript library.What parameters should our mock method expect? An example where spies would be interesting is if you’re passing in a collection of 5 items and want to make sure a method inside this loop that’s iterating the whole collection is called five times. The tools in Javascript’s own model help you do that a lot easier than you can in PHP. I have most commonly used spies for work where I test for Javascript. Mockery changed that a little bit where it sometimes doesn’t care how many times things are run. PHP unit combines these things where if you’re creating a test double using a get mock builder tool, you can also specify that you’re expecting it to be run a specific number of times. The concept in testing and test doubles in spies is you’ll be trying to verify that a specific thing was run. A stub creates a test double where I’ll tell it we have this test object that’s pretending to be another thing, I know that I’m expecting specific methods to be called, and when they are, I’m expecting specific results to come back.Īnother type of test double people sometimes use is spies. You just need it to exist to satisfy a parameter check as it’s passed around. A dummy creates a mock of an object that just has to stand in because you need that type of object to exist as a dependency, not because you need it to do anything. There are two types of test doubles the PHP community mostly uses: dummies and stubs. In the PHP community, these are all known as mocks because the tools says get mocks. Can you talk about the differences between Doubles, Dummies, Stubs, Spies, Mocks & Fakes and when to use each one? However, excessive mocking is a sign there is a problem with the code itself. Altogether, I mock anything I can’t control, and I try to look and see if there are ways to create a real version of a dependency with the test for things I can control. database connections, calls to remote API) and puts them into a proper state as a dependency. I do use mocks because mocks are the only way to test third-party things that aren’t under your control (e.g. It means you have a test scenario that’s really complicated and the object you’re trying to test can’t do it without a lot of help. Having to do excessive mocking is a test smell. If you start looking at what the object is responsible for doing and how the methods are supposed to be manipulating the state of the object, you’ll quickly realize you have some design mistakes. Everyone is familiar with the “God” object idea, which has large classes with 20~30 methods on them. If you’re having to do a lot of mocks in order to let a testing scenario work, chances are you have an object that you’re testing that is doing too much. I believe mocks are a really good way to tell if your code is overly complex. The text below is a summary done by the Codementor team and may vary from the original video and if you see any issues, please let us know!ĭo you advocate a lot of mocks? If so, do you ever get the feeling that you aren’t actually testing anything? How do you get past this feeling? Codementor PHP expert Chris Hartjes is a frequent conference speaker advocating testing, and he co-organized the GTA PHP User Group.Ĭhris recently joined us during Codementor’s Office Hours, and some of the users asked about testing doubles.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |