views:

622

answers:

3

I'm writing some functional tests for a controller in rails, using mocha to do mocking/stubbing.

Is there a way to prevent the template from being rendered during the test, so that I can test only the code in the controller?

It looks like rspec provides something like this, but I'm not using rspec.

A: 

Would render_to_string do what you need?

J Cooper
I'm not sure. That sounds like it would require me to modify the code in my controller. I'd just like to replace the render method with a mock while this test is running.
Starr Horne
+2  A: 

The most obvious solution seems to work:

@controller.expects(:render)

I could have sworn that I tried that last night with no luck. But this morning it works like a charm. I must have overlooked a typo.

Starr Horne
A: 

It doesn't look like using stub is necessary here. If you want to make sure that a given template is rendered, use assert_template and/or assert_response. You can also assert a state of the response object, either by hand or using helpers like assert_select.

Adam Byrtek
I understand your point, but what I was trying to do was a little different. Basically, I just wanted to stub a few methods in my model, and test to make sure that the controller called them correctly. By avoiding rendering, I'm really trying to avoid having to stub every method my view/helpers used
Starr Horne
I see, in this case mocking might be necessary, but it looks like you've already answered your own question :)
Adam Byrtek