我對第5章練習3 here替換爲full_title測試助手需要混淆Rails教程第5章練習3 - 原始full_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
還有同名一個軌輔助函數:
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
通過創建一個應用助手與直接測試功能: 規格/助手/ 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 helper函數,但我認爲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')
...
已在上面的函數調用總是指向實際軌功能,而不是respec功能?如果我能夠消除它首先是什麼?我覺得我在這裏錯過了一些東西。謝謝你的幫助。看起來像一個不好的主意來進行更改我不明白我的目標是何時學習Rails。
謝謝, 馬克
謝謝!很好的解釋。我很高興瞭解這個(以及更大的mixin概念)如何工作。昨天如此令人費解的事情現在清晰可見。 – Mark