【问题标题】:RoR , trouble with full_titleRoR,full_title 的问题
【发布时间】:2015-01-21 21:44:04
【问题描述】:

我在 Michael Hartl 的 RoR 教程的第 4 章,在使用 rspec 和使用 full_title 辅助函数时遇到了一些问题。在教程的第 4.1 部分中,我有如下帮助代码。

app/helpers/application_helper.rb

module ApplicationHelper

  # Returns the full title on a per-page basis.
  def full_title(page_title)
    base_title = "Ruby on Rails Tutorial Sample App"
    if page_title.empty?
      base_title
    else
      "#{base_title} | #{page_title}"
    end
  end
end

app/views/layouts/application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= stylesheet_link_tag "application", media: "all",
                                           "data-turbolinks-track" => true %>
    <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
    <%= csrf_meta_tags %>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

spec/requests/static_pages_spec.rb

require 'spec_helper'

describe "Static pages" do

  let(:base_title) {"Ruby on Rails Tutorial Sample App"}

  describe "Home page" do

    it "should have the content 'Sample App'" do
      visit '/static_pages/home'
      expect(page).to have_content('Sample App')
    end

    it "should have the base title" do
      visit '/static_pages/home'
      expect(page).to have_title("Ruby on Rails Tutorial Sample App")
    end

    it "should not have a custom page title" do
      visit '/static_pages/home'
      expect(page).not_to have_title('| Home')
    end
  end

end

app/views/static_pages/home.html.erb

<h1>Sample App</h1>
    <p>this is the home page for the <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
    sample application.</p>

当我尝试运行测试时,我得到了这个:

gvyntyk@gvyntyk-r60:~/rails_projects/sample_app$ rspec spec/requests/static_pages_spec.rb
FFF

Failures:

  1) Static pages Home page should not have a custom page title
     Failure/Error: visit '/static_pages/home'
     ActionView::Template::Error:
       undefined local variable or method `base_title' for #<#<Class:0xb0ea7f8>:0xb0ea1cc>
     # ./app/helpers/application_helper.rb:6:in `full_title'
     # ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___36363065_93308240'
     # ./spec/requests/static_pages_spec.rb:20:in `block (3 levels) in <top (required)>'

  2) Static pages Home page should have the base title
     Failure/Error: visit '/static_pages/home'
     ActionView::Template::Error:
       undefined local variable or method `base_title' for #<#<Class:0xb0ea7f8>:0x961e58c>
     # ./app/helpers/application_helper.rb:6:in `full_title'
     # ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___36363065_93308240'
     # ./spec/requests/static_pages_spec.rb:15:in `block (3 levels) in <top (required)>'

  3) Static pages Home page should have the content 'Sample App'
     Failure/Error: visit '/static_pages/home'
     ActionView::Template::Error:
       undefined local variable or method `base_title' for #<#<Class:0xb0ea7f8>:0x9c13244>
     # ./app/helpers/application_helper.rb:6:in `full_title'
     # ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___36363065_93308240'
     # ./spec/requests/static_pages_spec.rb:10:in `block (3 levels) in <top (required)>'

Finished in 1.23 seconds
3 examples, 3 failures

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:19 # Static pages Home page should not have a custom page title
rspec ./spec/requests/static_pages_spec.rb:14 # Static pages Home page should have the base title
rspec ./spec/requests/static_pages_spec.rb:9 # Static pages Home page should have the content 'Sample App'

谁能告诉我这里出了什么问题?

【问题讨论】:

  • 尝试将let 移动到第二个描述而不是第一个之后。
  • 我已经移动了 let,但我得到了同样的错误。即使我评论了这一行,我也会收到错误消息。

标签: ruby-on-rails ruby rspec


【解决方案1】:

感觉就像您没有发布测试所针对的确切代码。我没有看到您发布的代码是如何到达帮助程序中的 full_title 的,因为您在 application.html.erb 中只有 yeild(:title)。什么都没有调用 full_title。

查看书中的 Michaels 示例,您需要在标题标签中添加 full_title(yield(:title)) 以删除您在此处发布的内容。

【讨论】:

  • 我不专心,我编辑了我的鳕鱼,现在它可以正常工作了。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2020-10-22
  • 2012-05-06
  • 2011-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多