在rails中,如何在功能测试中存根化呈现方法?

时间:2021-02-20 03:53:05

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

我正在为rails中的控制器编写一些功能测试,使用mocha进行mock /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.

看起来rspec提供了类似的东西,但是我没有使用rspec。

3 个解决方案

#1


4  

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.

我可以发誓,我昨晚试过了,但运气不佳。但今天早上,它就像一种魅力。我一定是漏掉了一个错字。

#2


2  

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.

这里似乎不需要使用存根。如果希望确保呈现给定模板,请使用assert_template和/或assert_response。您还可以手动断言响应对象的状态,或者使用assert_select这样的助手。

#3


0  

Would render_to_string do what you need?

render_to_string会做您需要的吗?

#1


4  

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.

我可以发誓,我昨晚试过了,但运气不佳。但今天早上,它就像一种魅力。我一定是漏掉了一个错字。

#2


2  

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.

这里似乎不需要使用存根。如果希望确保呈现给定模板,请使用assert_template和/或assert_response。您还可以手动断言响应对象的状态,或者使用assert_select这样的助手。

#3


0  

Would render_to_string do what you need?

render_to_string会做您需要的吗?