2

我正在尝试使用 RSpec (2.x) 在控制器中测试渲染方法。这是我的控制器中的代码:

respond_to 做 |格式|
  format.html # index.html.erb
  format.json { 渲染 :json => @entities, :include => :properties, :overview => options[:overview] }
结尾

这里是我在我的规范文件中尝试的测试:

controller.should_receive(:render).with(hash_include(:overview => true))

问题是 RSpec 告诉我没有为渲染提供任何参数(“得到:(无参数)”)。甚至不是 :json 之一。如何正确存根渲染方法?

4

1 回答 1

4

如果你想测试你的render :json,只需检查响应是否包含 JSON 字符串。

简化示例:response.body.should == @object.to_json

如果您只想存根渲染方法,请使用controller.stub!(:render)

于 2011-01-19T18:06:46.873 回答