【问题标题】:Test the render method in a controller with RSpec使用 RSpec 在控制器中测试渲染方法
【发布时间】:2011-04-28 02:15:36
【问题描述】:

我正在尝试使用 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 之一。如何正确存根渲染方法?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 rspec rspec2


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多