ruby-on-rails - 如何测试渲染 :file => 'public/404.html' in Rspec 2?

标签 ruby-on-rails testing rspec

我在一个 Action 中有以下代码:

render :file => 'public/404.html' and return

这在浏览器中运行良好。我编写了以下 rspec 示例来对此进行测试:

  it "renders 404" do
    get :new
    response.should render_template('public/404.html')
  end

运行此示例会导致以下错误:

 Failure/Error: response.should render_template('public/404.html')
   Expected block to return true value.

我也尝试过 response.should render_template(:file => 'public/404.html') 但这也会导致错误。

我应该如何测试它?

最佳答案

你应该使用:

response.should render_template(:file => "#{Rails.root}/public/404.html")

关于ruby-on-rails - 如何测试渲染 :file => 'public/404.html' in Rspec 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11450692/

相关文章:

ruby-on-rails - 从 rspec Controller 规范中访问 Controller 实例变量

ruby-on-rails - rspec/测试 after_action

angularjs - Protractor 抛出超时异常后有没有办法停止它?

testing - 使用 Laika 和 PhantomJS 进行集成测试

ruby - Rspec:如何测试递归?

ruby-on-rails - RSpec::Core::ExampleGroup::Nested_2 中未定义的方法 `it'

ruby-on-rails - 从单独的进程调用 sidekiq 工作程序

ruby-on-rails - Rails API : One Request, 多个 Controller 操作

ruby-on-rails - form_for未定义的方法 `model_name'错误

testing - 使用 FakeRequests 和分块响应(枚举器)进行 Play2 测试是否存在错误?