RSpec集合是否包含空数组?
我在Post模型中有一个搜索方法:RSpec集合是否包含空数组?,rspec,Rspec,我在Post模型中有一个搜索方法: def self.search(search, category_id) if search.strip.empty? [] elsif category_id.empty? Post.approved.where('lower(title) LIKE ?', "%#{search.downcase.strip}%") else @category = Category.find_by('id = ?',
def self.search(search, category_id)
if search.strip.empty?
[]
elsif category_id.empty?
Post.approved.where('lower(title) LIKE ?', "%#{search.downcase.strip}%")
else
@category = Category.find_by('id = ?', category_id.to_i)
@category.posts.approved.where('lower(title) LIKE ?', "%#{search.downcase.strip}%")
end
end
这是我的post_spec.rb文件
require 'rails_helper'
RSpec.describe Post, type: :model do
let(:user) { create(:user) }
let(:category) { create(:category) }
let(:comments) { create(:comment) }
let(:likes) { create(:like) }
describe 'methods' do
describe 'search post by keyword or by category' do
let(:post_1) { create(:post, title: 'some example title') }
let(:post_2) { create(:post, title: 'another example title') }
it 'return post with title like keyword' do
expect(described_class.search('example', category.id.to_s)).to contain_exactly(post_1)
expect(described_class.search('EXAMPLE', category.id.to_s)).to contain_exactly(post_1)
expect(described_class.search('title', category.id.to_s)).to contain_exactly(post_1, post_2)
expect(described_class.search('foo', category.id.to_s)).to be_empty
end
end
end
end
我不知道为什么我的实际收集是一个空数组,而不是帖子
1) Post methods search post by keyword or by category return post with title like keyword
Failure/Error: expect(described_class.search('example', category.id.to_s)).to contain_exactly(post_1)
expected collection contained: [#<Post id: 152, title: "some example title"]
actual collection contained: []
the missing elements were: [#<Post id: 152, title: "some example title"]
1)Post方法按关键字或类别搜索Post返回具有类似标题的关键字的Post
失败/错误:期望(描述的类搜索('example',category.id.to_))。准确地包含(post_1)
预期收集包含:[#