所以这是我要测试的方法:
def self.by_letter(letter)
where("lastname LIKE ?", "#{letter}%").order(:lastname)
end
简单问一下,#{letter} 后面的百分号到底有什么作用?跟格式化有关系吗?
这是测试该方法的规范的一部分:
context 'method "by_letter"' do
it 'returns and ordered list by letter' do
theon = Contact.create!(
firstname: "Theon",
lastname: "Greyjoy",
email: "[email protected] /cdn-cgi/l/email-protection"
)
rob = Contact.create!(
firstname: "Rob",
lastname: "Stark",
email: "[email protected] /cdn-cgi/l/email-protection"
)
tyrion = Contact.create!(
firstname: "Tyrion",
lastname: "Lannister",
email: "[email protected] /cdn-cgi/l/email-protection"
)
result = Contact.by_letter("S")
expect(result).to include("Snow")
end
end
这是运行所述测试后得到的输出日志(哦,请记住,在规范的前面,我创建了一个“Jon Snow”,他应该按字母顺序出现在“Stark”之前):
Failures:
1) Contact method "by_letter" returns and ordered list by letter
Failure/Error: expect(result).to include("Snow")
expected #<ActiveRecord::Relation [#<Contact id: 1, firstname: "Jon", lastname: "Snow", email: "[email protected] /cdn-cgi/l/email-protection", created_at: "2014-11-14 17:17:55", updated_at: "2014-11-14 17:17:55">, #<Contact id: 3, firstname: "Rob", lastname: "Stark", email: "[email protected] /cdn-cgi/l/email-protection", created_at: "2014-11-14 17:17:56", updated_at: "2014-11-14 17:17:56">]> to include "Snow"
Diff:
@@ -1,2 +1,3 @@
-["Snow"]
+[#<Contact id: 1, firstname: "Jon", lastname: "Snow", email: "[email protected] /cdn-cgi/l/email-protection", created_at: "2014-11-14 17:17:55", updated_at: "2014-11-14 17:17:55">,
+ #<Contact id: 3, firstname: "Rob", lastname: "Stark", email: "[email protected] /cdn-cgi/l/email-protection", created_at: "2014-11-14 17:17:56", updated_at: "2014-11-14 17:17:56">]
我缺少什么?我的测试不应该通过吗,因为我返回了一个包含我指定的字符串的集合?是否存在一些复杂性,因为它不是常规数组而是某种代理数组?我需要做什么才能通过测试?