视图在浏览器中正确呈现,但辅助方法测试失败?
我有一个简单的视图和一个定义标题的助手。如果我在浏览器中拉出视图,一切正常,但 rspec 测试失败。这是我所拥有的:
这是我的测试:
describe PagesController do
render_views
before(:each) do
@base_title = "RoR Sample App"
end
describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
it "should have the right title" do
get 'home'
response.should have_selector("title",
:content => @base_title + " | Home")
end
end
end
页面控制器:
class PagesController < ApplicationController
def home
@title = "Home"
end
def contact
@title = "Contact"
end
def about
@title = "About"
end
def help
@title = "Help"
end
end
帮助器:
module ApplicationHelper
# Return a title on a per-page basis.
def title
base_title = "RoR22 Sample App"
if @title.nil?
base_title
else
"#{base_title} | #{@title}"
end
end
end
以及视图:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
它都在浏览器中正确呈现,但是当它到达 <%= title %>行。
1) PagesController GET 'home' should be successful
Failure/Error: get 'home'
ActionView::Template::Error:
undefined local variable or method `title' for #<#<Class:0xabf0b1c>:0xabeec18>
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___418990135_90147100_123793781'
# ./spec/controllers/pages_controller_spec.rb:12:in `block (3 levels) in <top (required)>'
知道我做错了什么吗?
I have a simple view and a helper that defines the title. Everything works fine if I pull up the view in the browser, but the rspec tests fail. Here's what I have:
Here are my tests:
describe PagesController do
render_views
before(:each) do
@base_title = "RoR Sample App"
end
describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
it "should have the right title" do
get 'home'
response.should have_selector("title",
:content => @base_title + " | Home")
end
end
end
The Pages Controller:
class PagesController < ApplicationController
def home
@title = "Home"
end
def contact
@title = "Contact"
end
def about
@title = "About"
end
def help
@title = "Help"
end
end
The helper:
module ApplicationHelper
# Return a title on a per-page basis.
def title
base_title = "RoR22 Sample App"
if @title.nil?
base_title
else
"#{base_title} | #{@title}"
end
end
end
And the view:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
It all renders properly in the browser, but the tests fail when it gets to <%= title %>
line.
1) PagesController GET 'home' should be successful
Failure/Error: get 'home'
ActionView::Template::Error:
undefined local variable or method `title' for #<#<Class:0xabf0b1c>:0xabeec18>
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___418990135_90147100_123793781'
# ./spec/controllers/pages_controller_spec.rb:12:in `block (3 levels) in <top (required)>'
Any idea what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 spork 的一个已知问题。您可以在 prefork 块中使用此解决方法:
This is a known issue with spork. You can use this workaround in your prefork block:
这就是您的
application.html.erb
。它在PagesHelper
中看不到该方法,您需要将该方法放入ApplicationHelper
中。只有PagesController
呈现的视图才能看到PagesHelper
。在助手中显式添加return
是个好主意。That's your
application.html.erb
. It can't see that method in thePagesHelper
, you need to put that method in yourApplicationHelper
. Only the views rendered by yourPagesController
can seePagesHelper
. And it's a good idea to explicitly addreturn
in your helpers.最终的问题是,我使用
Spork
gem 来加速我的测试,并且出于某种原因spork
没有注册ApplicationHelper
> 已经改变了。解决方案只是重新启动
spork
gem 并重新运行测试。It ends up the problem is that I was using the
Spork
gem to speed up my testing and for whatever reasonspork
wasn't registering that theApplicationHelper
had changed.The solution was simply to reboot the
spork
gem and rerun the tests.