Ruby on rails 3 Rails教程第5章练习3-最初的完整标题函数做了什么?

Ruby on rails 3 Rails教程第5章练习3-最初的完整标题函数做了什么?,ruby-on-rails-3,rspec-rails,railstutorial.org,Ruby On Rails 3,Rspec Rails,Railstutorial.org,我对第5章练习3感到困惑,它取代了对完整标题测试助手的需求 spec/support/utilities.rb: 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 还有一个同名的rails助手函数: modul

我对第5章练习3感到困惑,它取代了对完整标题测试助手的需求

spec/support/utilities.rb:

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
还有一个同名的rails助手函数:

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
通过创建一个应用程序帮助程序,直接使用以下工具测试函数: 等级库/辅助程序/应用程序\u辅助程序\u等级库.rb

require 'spec_helper'

describe ApplicationHelper do

 describe "full_title" do
   it "should include the page title" do
     full_title("foo").should =~ /foo/
   end

   it "should include the base title" do
     full_title("foo").should =~ /^Ruby on Rails Tutorial Sample App/
   end

   it "should not include a bar for the home page" do
     full_title("").should_not =~ /\|/
   end
  end
end
它直接测试rails助手函数非常好,但我认为utilities.rb中的完整title函数是用于Rspec代码的。因此,为什么我们可以在utilities.rb中删除上述代码,并替换为:

include ApplicationHelper
我做了交换,一切都还顺利。我希望Rspec代码在使用Rspec函数时出现如下错误,但事实并非如此:

it "should have the right links on the layout" do
  visit root_path
  click_link "About"
  page.should have_selector 'title', text: full_title('About Us')
  ...
上述函数调用是否始终指向实际的rails函数而不是respec函数?如果我能消除它,它最初是为了什么?我觉得我错过了一些东西。谢谢你的帮助。当我的目标是学习Rails时,进行我不理解的更改似乎是个坏主意

谢谢,
在specs中始终标记spec/support/utilities.rb调用中的

完整标题

在将代码替换为
include ApplicationHelper
之前,规范中的
full\u title
正在调用utilities.rb中的函数:

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
通过将代码替换为

include ApplicationHelper
要明确的是,你实际上包括了

module ApplicationHelper
来自helpers/application_helper.rb

这与spec/helpers/application\u helper\u spec.rb中的
描述ApplicationHelper
无关

require 'spec_helper'

describe ApplicationHelper do

 describe "full_title" do
   it "should include the page title" do
     full_title("foo").should =~ /foo/
   end

   it "should include the base title" do
     full_title("foo").should =~ /^Ruby on Rails Tutorial Sample App/
   end

   it "should not include a bar for the home page" do
     full_title("").should_not =~ /\|/
   end
  end
end
真正发生的是
模块ApplicationHelper
中的
full\u title
函数现在转到utilities.rb。因此,utilities.rb从
模块ApplicationHelper
(helpers/application\u helper.rb)获得对函数
完整标题的访问权


因此,当规范调用
full_title
函数时,它是从utilities.rb调用的,这是可能的,因为该函数是通过使用
include ApplicationHelper

混合在一起的。不幸的是,该练习说:

通过 为原始helper方法编写测试,如清单1所示 5.41. (您必须创建spec/helpers目录和应用程序_helper_spec.rb文件。)然后使用 清单5.42中的代码

…这是完全错误的

一些预备工作:

app/helpers/application_helper.rb中的full_title()方法可用于整个应用程序中的常规代码,而不是rspec测试。spec/support/utilities.rb中添加了full_title()方法,以便rspec测试可以调用它

术语:

应用程序助手=app/helpers/application\u helper.rb中的完整标题方法

rspec helper=spec/support/utilities.rb中的完整标题方法

为应用程序助手编写测试并不能消除对rspec助手的需求——因为为一种方法编写测试并不能神奇地消除对其他方法的需求。课文应该这样说:

您可以消除对rspec助手的需要,它是一个 通过替换 带有以下include语句的rspec帮助程序:

目前,, 您可以假装包含模块会在 位于include语句点的模块。application helper恰好是在名为ApplicationHelper的模块中定义的——打开文件app/helpers/application\u helper.rb并查看一下

接下来,编写测试 应用程序助手可以确保其正常工作


谢谢很好的解释。我很高兴了解这个(以及更大的mixin概念)现在是如何工作的。昨天如此令人困惑的事情现在已经一清二楚了。