4

我对这里的第 5 章练习 3 感到困惑,它取代了对 full_title 测试助手的需求

规范/支持/实用程序.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

通过创建一个应用程序助手来直接测试函数:spec/helpers/application_helper_spec.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 中的完整标题函数用于 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 时,我不明白进行更改似乎是个坏主意。

谢谢,马克

4

2 回答 2

4

full_title在规范中总是从spec/support/utilities.rb调用。

在用 替换代码之前include ApplicationHelperfull_title在规范中调用了实用程序.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

This has nothing to do with describe ApplicationHelper in spec/helpers/application_helper_spec.rb

What really happens is that the full_title function from module ApplicationHelper is now mixed in (see Mixins) to utilities.rb. Hence, utilities.rb gains access to the function full_title from module ApplicationHelper(helpers/application_helper.rb).

So, when the specs call the full_title function, it is called from utilities.rb, which is possible because the function has been mixed in via the use of include ApplicationHelper.

于 2013-04-20T13:22:27.780 回答
0

Unfortunately, the exercise says:

Eliminate the need for the full_title test helper in Listing 5.29 by writing tests for the original helper method, as shown in Listing 5.41. (You will have to create both the spec/helpers directory and the application_helper_spec.rb file.) Then include it into the test using the code in Listing 5.42.

...which is completely wrong.

Some preliminaries:

The full_title() method in app/helpers/application_helper.rb is a method that is available to regular code throughout the application--not the rspec tests. The full_title() method in spec/support/utilities.rb was added so that the rspec tests could call it.

Terminology:

application helper = the full_title method in app/helpers/application_helper.rb

rspec helper = the full_title method in spec/support/utilities.rb

Writing tests for the application helper does not eliminate the need for the rspec helper--because writing a test for one method does not magically eliminate the need for some other method. What the text should say is something like this:

You can eliminate the need for the rspec helper, which is a duplicate of the application helper, by replacing the rspec helper with the following include statement:

include ApplicationHelper

For the time being, you can pretend that including a module inserts the methods in the module at the point of the include statement. The application helper happens to be defined inside a module named ApplicationHelper--open the file app/helpers/application_helper.rb and take a look.

Next, write tests for the application helper to ensure that it works correctly.

于 2013-08-05T14:31:38.187 回答