使用ActiveRecord :: Relation时的RSpec匹配器 [英] RSpec Matchers When Working With ActiveRecord::Relation

查看:100
本文介绍了使用ActiveRecord :: Relation时的RSpec匹配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我要测试的方法:

So here's the method I want to test:

def self.by_letter(letter)
  where("lastname LIKE ?", "#{letter}%").order(:lastname)
end

这里的简单问题是,在#{letter}之后百分号究竟是什么?

Quick question here, what exactly does the percent sign after #{letter} do? Something to do with formatting?

这是规范中测试该方法的一部分:

Here's part of the spec that tests that method:

    context 'method "by_letter"' do
    it 'returns and ordered list by letter' do
      theon = Contact.create!(
        firstname: "Theon", 
        lastname: "Greyjoy", 
        email: "tgreyjoy@ironprice.com"
        )
      rob = Contact.create!(
       firstname: "Rob", 
       lastname: "Stark", 
       email: "rstark@winterfell.com" 
       )
      tyrion = Contact.create!(
       firstname: "Tyrion", 
       lastname: "Lannister", 
       email: "tlannister@kingslanding.com" 
       )
      result = Contact.by_letter("S")
      expect(result).to include("Snow")
    end
  end

这是我得到的日志运行上述测试后的输出(哦,请记住,在规范的前面,我创建了一个乔恩·雪诺,他应该弹出b在 Stark之前按字母顺序):

And here's the logs I get for an output after running said test (oh, bare in mind, earlier in the spec I created a "Jon Snow", and he should pop up before "Stark" alphabetically):

    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: "lordcommander@nightswatch.com", created_at: "2014-11-14 17:17:55", updated_at: "2014-11-14 17:17:55">, #<Contact id: 3, firstname: "Rob", lastname: "Stark", email: "rstark@winterfell.com", 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: "lordcommander@nightswatch.com", created_at: "2014-11-14 17:17:55", updated_at: "2014-11-14 17:17:55">,
       + #<Contact id: 3, firstname: "Rob", lastname: "Stark", email: "rstark@winterfell.com", created_at: "2014-11-14 17:17:56", updated_at: "2014-11-14 17:17:56">]

我想念什么?我返回包含一个指定字符串的集合,是否应该通过测试?是否有一些复杂性,因为它不是常规数组而是某种代理数组?我需要做些什么才能使我的考试通过?

What am I missing? Shouldn't my test pass because I return a collection that includes a string I specified? Is there some complication because it's not a regular array but some sort of proxy array? What do I need to do to get my test to pass?

推荐答案

您的结果 ActiveRecord :: Relation 对象。因此,您应该执行以下操作:-

Your result is an ActiveRecord::Relation object. So you should do as below :-

expect(result).to include(rob)

rob 的姓氏为 Stark ,因此 Contact.by_letter( S)将在中包含 rob 已过滤列表。

rob has the last name as "Stark", thus Contact.by_letter("S") will include rob in the filtered list.

这篇关于使用ActiveRecord :: Relation时的RSpec匹配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆