Closed tarrsalah closed 4 years ago
How to mock the DocumentManager?
Don't. Only mock what you own. I don't know what you want to do in tests, but mocking the document manager is the wrong way to go at it.
@alcaeus Thanks for the response, I'm trying to mock the DocumentManager
in functional tests, (Stackoverflow question with more details https://stackoverflow.com/questions/60805292/how-to-mock-symfony-mongodb-documentmanager-service)
To be more specific as to what I was trying to say above: you shouldn't try to mock an object manager. What exactly are you trying to accomplish?
I'm trying to test a controller method that persist a document, I want to test this controller method without hitting the MonogoDB server. So I tried to replace the real document manager with a mock that assert that the persist
method is called.
What do you thing of the following approach please?
ProductService
for example) around the document manager.In that case, you'd be wrapping the document manager for the sole purpose of mocking it: it's just as bad. Instead, just write the data to the database, and check that the data has been persisted correctly. Not only does that test your controller, but also your mapping to ensure data is mapped correctly and all necessary data is written.
Thanks for your replies (Learned a lot from them ).
thank you both for this. very helpful.
I started by aliasing the document manager in
services_test.yaml
with this entryThen I replace it during the test
The test fails because the mock method is never called, and the controller uses the real document manager.