1

I'm trying to write some functional tests in Symfony2 with MongoDB, but I can't figure out the problem that I am having. In test I edit an object via form, and than fetch it with the document manager, but I keep getting unchanged data. If I look into DB, the data is properly changed.

What am I missing?

4

1 回答 1

3

It would help to see your full test case, but I assume you're doing the following:

  • Fetching the object via the document manager service in your test case
  • Simulating a form submission using the HTTP test client (see: here)
  • Fetching the object via the document manager again

Since changes are showing up in the database, it's safe to assume that the form is properly binding and your controller is properly flushing the changes. Assuming the form submission happens within its own kernel context, the document manager it uses internally is not the same instance as the document manager you're using explicitly in your test case.

If you're doing an initial fetch before the document is modified, your document manager will not be aware of any changes made to the document elsewhere unless you refresh() the document or clear() the document manager of all managed documents before re-querying. The detaching documentation explains this a bit more.

If the bullets above correctly describe your test case, I would suggest clearing the document manager before re-querying and asserting the result. Alternatively, you could probably change the test to avoid querying at all before simulating the form submission. Any test fixtures you're using can likely be abstracted out of the test case itself (e.g. inserted during a setUp() method, after which you clear the document manager).

于 2012-08-14T15:08:02.327 回答