【发布时间】:2013-04-18 16:55:23
【问题描述】:
我对第 5 章练习 3 here 感到困惑,它取代了对 full_title 测试助手的需求
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
通过创建一个应用程序助手来直接测试函数: 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 辅助函数,但我认为 utility.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 时,我不明白进行更改似乎是个坏主意。
谢谢, 标记
【问题讨论】:
标签: ruby-on-rails-3 rspec-rails railstutorial.org