关于让 rspec 控制器测试通过代码正确的地方确实很奇怪的问题
我有一个奇怪的问题,我知道代码可以工作,如果我单独运行该 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
这里的想法是“show”操作应该使用参数给出的任何名称来渲染模板,但如果它不存在,我们希望将用户发送到通用 404页。
现在,我可以在 /webpages 视图目录中复制 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 测试总是失败,尽管我个人知道这很好。
感谢您的帮助
I have a weird problem where I know the code works, the RSpec test will pass if I run that Spec file by itself, but it fails when I run all the tests in the entire suite (everything in /specs).
Here is the test:
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
Here's the code:
class WebpagesController < ApplicationController
def show
begin
render(params[:page])
rescue ActionView::MissingTemplate
render("/public/404")
end
end
end
The idea here is that the 'show' action should render the template with whatever name is given by the parameter, but if it doesn't exist, we want to send the user to the generic 404 page.
Now, I could just duplicate the 404 template in the /webpages view directory, but I really want to figure out how I can get this to pass using the one provided in the /public folder like I am trying to do here.
If I run the test in isolation - it PASSES. If I run the test with all the others, I get the following error:
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)>'
I've honestly been stumped with this one for several days, and I've just been working on other stuff... but I am a little annoyed to see 1 test failing all the time even though I personally know it's fine.
Thanks for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误实际上是说您的
render('/public/404')
行是引发错误的行。尝试渲染 404 文件的完整路径,而不仅仅是
/public/404/
:The error is actually saying that your
render('/public/404')
line is the one throwing the error.Try rendering the full path to the 404 file instead of just
/public/404/
: