은 여기에서 설명 : 여기에RSpec에 테스트 인스턴스 방법은 객체 메소드 호출하면 나는 class_double 방법을 사용하려고 시도하고
https://relishapp.com/rspec/rspec-mocks/docs/verifying-doubles/using-a-class-double 내 코드입니다 :
search.rb
class Search < ActiveRecord::Base
def find_events
events = Event.find_by_dates(beginning_date, end_date)
end
end
event.rb
class Event < ActiveRecord::Base
scope :find_by_dates, ->(starting_date , ending_date) { where('(date(start_datetime) >= ? AND date(end_datetime) <= ?)', starting_date.to_date, ending_date.to_date)}
end
search_spec.rb
,363,210let(:march1) {DateTime.parse('1st March 2014')}
let(:march3) {DateTime.parse('3rd March 2014')}
let(:march_search) {create(:search, beginning_date: march1, end_date: march3)}
let(:mar1_event) {create(:event,
start_datetime: DateTime.parse('1st March 2014 04:00:00 PM'),
end_datetime: DateTime.parse('1st March 2014 06:00:00 PM'))}
let(:mar2_event) {create(:event,
start_datetime: DateTime.parse('2nd March 2014 04:00:00 PM'),
end_datetime: DateTime.parse('2nd March 2014 06:00:00 PM'))}
let(:mar1_to_3_event) {create(:event,
start_datetime: DateTime.parse('1st March 2014 04:00:00 PM'),
end_datetime: DateTime.parse('3rd March 2014 06:00:00 PM'))}
let(:march_search) {create(:search, beginning_date: march1, end_date: march3)}
describe 'find_events method' do
it 'should call Event.find_by_dates' do
event = class_double("Event").as_stubbed_const(:transfer_nested_constants => true)
expect(event).to receive(:find_by_dates).with(march1, march3)
march_search.find_events
end
it 'should return events within a date range' do
events = march_search.find_events
expect(events).to include(mar1_event, mar2_event, mar1_to_3_event)
end
it 'it should not return events outside of the date range' do
events = sept_search.find_events
expect(events).to_not include(mar1_event, mar2_event, mar1_to_3_event)
end
지금까지 아무 문제없는 나는 문자열을 어떤 결과를 찾을 수 없습니다 있는지 확인하고 반환 할 때까지
다음def find_events
events = Event.find_by_dates(beginning_date, end_date)
if events.empty?
return "SORRY NO RESULTS FOUND"
end
end
내가받을 다음과 같은 오류
실패 :
1) Search find_events method it should not return events outside of the date range
Failure/Error: expect(events).to_not include(mar1_event, mar2_event, mar1_to_3_event)
TypeError:
no implicit conversion of Event into String
# ./spec/models/search_spec.rb:93:in `block (3 levels) in <top (required)>'
2) Search find_events method should call Event.find_by_dates
Failure/Error: march_search.find_events
NoMethodError:
undefined method `empty?' for nil:NilClass
# ./app/models/search.rb:41:in `find_events'
# ./spec/models/search_spec.rb:84:in `block (3 levels) in <top (required)>'
3) Search find_events method should return events within a date range
Failure/Error: expect(events).to include(mar1_event, mar2_event, mar1_to_3_event)
TypeError:
no implicit conversion of Event into String
# ./spec/models/search_spec.rb:88:in `block (3 levels) in <top (required)>'
Event.find_by_dates가 0인지 확인했지만 다음 오류가 발생했습니다. 정의되지 않은 메서드`include? ' nil 클래스의 경우
빈 ActiveRelation 객체가 반환되어 검색을 계속할 수 없는지 확인해야합니다. 아무도 비어있는 검색 결과를 테스트하는 방법을 말해 줄 수 있습니까?
감사합니다.
나는 이미 Event.find_by_dates를 테스트했지만, 이제는 search.find_events가 호출 될 때 그 메소드와 몇 개의 다른 메소드가 함께 연결될 때 어떤 일이 발생하는지 확인해야한다. 또한 Event 클래스 메소드 호출되고 있습니다. –