我正在阅读railstutorial chapter 5.6.4。
根据页面,以下两个代码提供相同的测试。
但我无法理解为什么它没有参数page_title
。
Rspec中的"foo"
字符串是否有特殊含义?
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
spec/helpers/application_helper_spec.rb
require 'spec_helper'
describe ApplicationHelper do
describe "full_title" do
it "should include the page title" do
expect(full_title("foo")).to match(/foo/)
end
it "should include the base title" do
expect(full_title("foo")).to match(/^Ruby on Rails Tutorial Sample App/)
end
it "should not include a bar for the home page" do
expect(full_title("")).not_to match(/\|/)
end
end
end
spec/support/utilities.rb
include ApplicationHelper
答案 0 :(得分:1)
不,字符串"foo"
对RSpec没有任何特殊含义,它只是在测试中用作检查full_title
帮助程序是否正常工作的示例。
要回答问题的其他部分,如果没有传入页面标题,则if语句将采用第一个路径,因为page_title
变量为空,并且您将仅返回基本标题。以下是每个测试实际执行的操作:
# This is expected to return "Ruby on Rails Tutorial Sample App | foo", which
# will match /foo/.
it "should include the page title" do
expect(full_title("foo")).to match(/foo/)
end
# This returns the same as above ("Ruby on Rails Tutorial Sample App | foo"),
# but this test is checking for the base title part instead of the "foo" part.
it "should include the base title" do
expect(full_title("foo")).to match(/^Ruby on Rails Tutorial Sample App/)
end
# This returns just "Ruby on Rails Tutorial Sample App" because the page title
# is empty. This checks that the title doesn't contain a "|" character but that
# it only returns the base title.
it "should not include a bar for the home page" do
expect(full_title("")).not_to match(/\|/)
end
答案 1 :(得分:1)
这是可能对您有帮助的测试的“rspec to English”翻译:
如果我为full_title
方法提供字符串"foo"
,则结果应为:
"foo"
"Ruby on Rails Tutorial Sample App"
"|"
测试背后的想法是确保您的代码使用一些有意义的代码行为示例。您无法测试每种可能的情况,因此您选择一种(或多种)最佳方法来描述方法的功能。
在这种情况下,它会在编程中传递一个字符串参数"foo"
,通常为used as a placeholder。