I started to write Doctrine 2 Mongo ODM unit tests but realized I didn’t have a good strategy in my code to do this. I want to run the tests and actually persist the objects but I then want to allow my test data to be easily remove in tearDown. Collection and DB names must be specified from what I’ve seen in the annotations and can’t be overridden so I can’t just create a test DB and wipe it out later.
Does anyone have best practices or examples of what they think the best ways to test?
You don’t have to persist your objects. The good way is use mock to check if your object was persisted. I’ll give you some example. Let say you have a class:
Now, you won’t check if a document was really persisted because this is tested somewhere id Doctrine code. You can assume that
persistandflushmethods work fine. The thing you want to check is if your code calls these methods correctly.So, your test could looks like: