我有一个奇怪的问题,我知道代码可以工作,如果我自己运行那个 Spec 文件,RSpec 测试将通过,但是当我运行整个套件中的所有测试(/specs 中的所有内容)时,它会失败。
这是测试:
require 'spec_helper'
describe WebpagesController do
include Devise::TestHelpers
render_views
describe "GET 'show'" do
it "should render the template if it exists" do
get 'show', :page => "tour"
response.should render_template("tour")
end
it "should render 404 page if template does not exist" do
expect {
get 'show', :page => 'does_not_exist'
}.to_not raise_error(ActionView::MissingTemplate)
response.should render_template("/public/404")
end
end
end
代码如下:
class WebpagesController < ApplicationController
def show
begin
render(params[:page])
rescue ActionView::MissingTemplate
render("/public/404")
end
end
end
这里的想法是“显示”操作应该使用参数给定的任何名称呈现模板,但如果它不存在,我们希望将用户发送到通用 404 页面。
现在,我可以在/webpages View 目录中复制 404 模板,但我真的想弄清楚如何使用/public 文件夹中提供的模板来传递它,就像我在这里尝试做的那样。
如果我单独运行测试 - 它会通过。如果我与所有其他人一起运行测试,我会收到以下错误:
expected no ActionView::MissingTemplate, got #<ActionView::MissingTemplate: Missing template /public/404 with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/home/egervari/Projects/training/app/views", "/usr/local/lib/ruby/gems/1.9.1/gems/devise-1.3.4/app/views", "/home/egervari/Projects/training/spec", "/">
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.5.0/lib/rspec/expectations/fail_with.rb:29:in `fail_with'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.5.0/lib/rspec/expectations/handler.rb:44:in `handle_matcher'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.5.0/lib/rspec/expectations/extensions/kernel.rb:50:in `should_not'
/home/egervari/Projects/training/spec/controllers/webpages_controller_spec.rb:17:in `block (3 levels) in <top (required)>'
老实说,我已经被这个问题难住了好几天了,而且我一直在研究其他东西......但我有点生气看到 1 测试一直失败,即使我个人知道这很好.
感谢帮助
最佳答案
错误实际上是说您的 render('/public/404')
行是引发错误的行。
尝试呈现 404 文件的完整路径,而不仅仅是 /public/404/
:
render("#{Rails.root}/public/404.html")
关于ruby-on-rails - 让 rspec Controller 测试通过代码正确的地方真的很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5997865/