我正在尝试在我的 ActiveRecord
Flight
对象范围(例如 #around_date
)上编写 rspec-rails 测试。这是我写的一个测试:
let(:flight) { create(:flight) }
date = DateTime.new(year=2023, month=12, day=25)
it 'selects within default date range' do
flights = Flight.around_date(date.to_s).to_a
expect flights.to include(flight)
end
这应该测试
#around_date
作用域将返回使用 Flight
创建的 let
对象。 (工厂创建日期为 12-25-2023)。
这是
#around_date
模型中 Flight
范围的代码:
scope :around_date, ->(date_string, day_interval = 1) {
if date_string.present?
date = Date.parse(date_string)
lower_date = date - day_interval.days
upper_date = date + day_interval.days
where(start_datetime: (lower_date..upper_date))
end
}
当我运行测试时,我收到以下错误消息:
Failure/Error: expect flights.to include(created_flights)
NoMethodError:
undefined method `>=' for #<RSpec::Matchers::BuiltIn::ContainExactly:0x00007fe400279098 @expected=[#<Flight id: 1, departure_airport_id: 2, arrival_airport_id: 1, start_datetime: "2023-12-25 00:00:00.000000000 +0000", duration_minutes: 30, created_at: "2023-12-02 16:51:32.363329000 +0000", updated_at: "2023-12-02 16:51:32.363329000 +0000">]>
# ./spec/models/flight_spec.rb:11:in `block (4 levels) in <main>'
我不知道这个错误消息的含义,因为相关代码中的任何地方都没有
>=
的情况。
任何关于此错误消息可能意味着什么的想法将不胜感激!
您的测试应该是:
let(:flight) { create(:flight) }
date = DateTime.new(year=2023, month=12, day=25)
it 'selects within default date range' do
flights = Flight.around_date(date.to_s).to_a
expect(flights).to include(flight)
end