Rails 中辅助方法的问题

发布于 2024-12-01 07:25:21 字数 2154 浏览 3 评论 0原文

我有以下辅助方法(app/helpers/application_helper.rb):

module ApplicationHelper

 #Return a title on a per-page basis
 def title
   base_title = "Ruby on Rails Tutorial Sample App"
   if @title.nil?
     base_title
   else
     "#{base_title} | #{@title}"
   end
 end
end

这是 erb(app/views/layouts/application.html.erb):

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <%= csrf_meta_tag %>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

我运行了 rspec 测试来查看此辅助方法是否有效,并且似乎它找不到标题。

这是错误消息:

Failures:

  1) PagesController GET 'home' should be successful
     Failure/Error: get 'home'
     ActionView::Template::Error:
       undefined local variable or method `title' for #<#<Class:0x991ecb4>:0x991315c>
     # ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb__248109341_80250010__979063050'
     # ./spec/controllers/pages_controller_spec.rb:8:in `block (3 levels) in <top (required)>'

  2) PagesController GET 'home' should have the right title
     Failure/Error: get 'home'
     ActionView::Template::Error:
       undefined local variable or method `title' for #<#<Class:0x991ecb4>:0x9d7d094>
     # ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb__248109341_82566280__979063050'
     # ./spec/controllers/pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

任何人都可以告诉我我做错了什么吗?

更新:

我通过执行以下操作包含了帮助程序:

 describe PagesController do
      include ApplicationHelper
      render_views

      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 => "Ruby on Rails Tutorial Sample App | Home")
    end
  end


//and some more

但是我仍然遇到相同的错误

I have the following helper method (app/helpers/application_helper.rb):

module ApplicationHelper

 #Return a title on a per-page basis
 def title
   base_title = "Ruby on Rails Tutorial Sample App"
   if @title.nil?
     base_title
   else
     "#{base_title} | #{@title}"
   end
 end
end

and here's the erb ( app/views/layouts/application.html.erb):

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <%= csrf_meta_tag %>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

I ran an rspec test to see if this helper method works and it seems that it can't find title.

Here's the error message:

Failures:

  1) PagesController GET 'home' should be successful
     Failure/Error: get 'home'
     ActionView::Template::Error:
       undefined local variable or method `title' for #<#<Class:0x991ecb4>:0x991315c>
     # ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb__248109341_80250010__979063050'
     # ./spec/controllers/pages_controller_spec.rb:8:in `block (3 levels) in <top (required)>'

  2) PagesController GET 'home' should have the right title
     Failure/Error: get 'home'
     ActionView::Template::Error:
       undefined local variable or method `title' for #<#<Class:0x991ecb4>:0x9d7d094>
     # ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb__248109341_82566280__979063050'
     # ./spec/controllers/pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

Can anyone tell me what I did wrong?

UPDATE:

I included the helper by doing the following:

 describe PagesController do
      include ApplicationHelper
      render_views

      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 => "Ruby on Rails Tutorial Sample App | Home")
    end
  end


//and some more

However I am still getting the same error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦断已成空 2024-12-08 07:25:21

在您看来,默认情况下不包括帮助程序。

您可以使用模板对象模拟辅助方法

template.should_receive(:title).and_return("Title")

然后您可以测试您的助手 分别。

或者,您可以通过简单地执行以下操作将助手包含在视图规范中:

include ApplicationHelper

编辑

describe PagesController do
  include ApplicationHelper

  describe "GET 'home'" do
    it "should be successful" do
      controller.template.should_receive(:title).and_return("Title")
      get 'home'
      response.should be_success
    end
  end
end

In your views, the helpers are not included by default.

You can mock out the helper methods using the template object:

template.should_receive(:title).and_return("Title")

You can then test your helpers separately.

Alternatively you can include your helpers in your view spec by simply doing:

include ApplicationHelper

EDIT

describe PagesController do
  include ApplicationHelper

  describe "GET 'home'" do
    it "should be successful" do
      controller.template.should_receive(:title).and_return("Title")
      get 'home'
      response.should be_success
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文