Как написать тест по scope?
есть такой scope
class Item < ApplicationRecord
has_many :reviews, dependent: :destroy
has_many :bookings, dependent: :destroy
belongs_to :city
scope :get_all_items_in_city, ->(city) { where(city: city) }
end
по нему мне нужно сделать тест используя rspec-rails и factory_bot
есть такой набросок
RSpec.describe Item, type: :model do
describe ".get_all_items_in_city" do
let(:city) {'test'}
it 'should gets all items in city' do
end
end
end
Ответы (1 шт):
Автор решения: Taras
→ Ссылка
RSpec.describe Item, type: :model do
describe ".get_all_items_in_city" do
subject { described_class.get_all_items_in_city(city) }
let(:city) { 'New York' }
let!(:expected_cities) { create_list(:item, 2, city: city) }
let!(:unexpected_cities) { create(:item, city: 'Random City') }
it 'should return correct count of items' do
expect(subject.count).to eq(2)
end
it 'should return correctlist of items' do
expect(subject.to_a).to match_array(expected_cities)
end
end
end