How can I unit test a Symfony2 controller?

前端 未结 4 960
暖寄归人
暖寄归人 2021-01-31 16:42

I want to use Test Driven Development as much as possible — it\'s a great way of working.

I am troubled by the fact that Symfony2 controllers create and return a new

4条回答
  •  一个人的身影
    2021-01-31 17:25

    Lewis - I thought I'd jump in here. The approach above has you replicating the better part of your actions logic in your tests. There's nothing wrong with this, many frameworks (notably RSPEC in Rails) actually suggest you perform both Unit tests on your Controller objects as well as functional tests. However, given your example, I think I'd skip the unit test and go for the functional approach.

    The point of a test in my mind is to create a sandboxed environment, run the test, and check for side effects and direct result. If you get to a point where most of your test is isolating the method, then its probably time for either a different testing approach or a different approach to writing your class. Given that this is a controller, and by nature they glue together different pieces of the stack, I'd create your sandbox farther up the stack. Specifically, I would use an approach like this:

    https://github.com/PolishSymfonyCommunity/SymfonyMockerContainer

    Worked great for me :)

提交回复
热议问题