视图在浏览器中正确呈现,但辅助方法测试失败?

发布于 2024-11-19 01:26:17 字数 1917 浏览 2 评论 0原文

我有一个简单的视图和一个定义标题的助手。如果我在浏览器中拉出视图,一切正常,但 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终止放荡 2024-11-26 01:26:17

这是 spork 的一个已知问题。您可以在 prefork 块中使用此解决方法:

Spork.trap_method(Rails::Application, :reload_routes!)
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)

This is a known issue with spork. You can use this workaround in your prefork block:

Spork.trap_method(Rails::Application, :reload_routes!)
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
祁梦 2024-11-26 01:26:17

这就是您的 application.html.erb。它在 PagesHelper 中看不到该方法,您需要将该方法放入 ApplicationHelper 中。只有 PagesController 呈现的视图才能看到 PagesHelper。在助手中显式添加 return 是个好主意。

That's your application.html.erb. It can't see that method in the PagesHelper, you need to put that method in your ApplicationHelper. Only the views rendered by your PagesController can see PagesHelper. And it's a good idea to explicitly add return in your helpers.

口干舌燥 2024-11-26 01:26:17

最终的问题是,我使用 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 reason spork wasn't registering that the ApplicationHelper had changed.

The solution was simply to reboot the spork gem and rerun the tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文