2012-02-27 2 views
1

RSpec (2.8.0) 커스텀 matcher 기능에서 카운터 직관적 인 동작을 보았습니다. 버그인지 기능인지 혼란 스럽습니다. 코드를 살펴 보자 :RSpec 커스텀 matcher는 사양 사이에 상태를 유지합니다.

# matcher code 
RSpec::Matchers.define :exist do 
    chain :with_start_time do |time| 
    @start_time = time 
    end 

    chain :with_end_time do |time| 
    @end_time = time 
    end 

    match do |subject| 
    result = true 
    result &&= subject.start_time == @start_time if @start_time 
    result &&= subject.end_time == @end_time if @end_time 
    result 
    end 

    failure_message_for_should do |subject| 
    "Failure!\n".tap do |msg| 
     if @start_time != subject.start_time 
     msg << "Expected start_time to be #@start_time but was #{subject.start_time}\n" 
     end 
     if @end_time != subject.end_time 
     msg << "Expected end_time to be #@end_time but was #{subject.end_time}\n" 
     end 
    end 
    end 
end 

#spec code 
require 'ostruct' 

describe 'RSpec custom matcher keeping state between tests' do 
    let(:time) { Time.now } 

    it 'passes the first spec' do 
    o = OpenStruct.new(start_time: time) 
    o.should exist.with_start_time(time) 
    end 

    it 'fails the second because matcher kept @start_time from the first test' do 
    o = OpenStruct.new(end_time: time) 
    o.should exist.with_end_time(time) 
    end 
end 

이 (문제 시연) 실패

avetia01:~/projects/custom_matcher_bug% rspec test_spec.rb    
.F 

Failures: 

    1) RSpec custom matcher keeping state between tests fails the second because matcher kept @start_time from the first test 
    Failure/Error: o.should exist.with_end_time(time) 
     Failure! 
     Expected start_time to be 2012-02-27 12:20:25 +0000 but was 
    # ./test_spec.rb:41:in `block (2 levels) in <top (required)>' 

Finished in 0.00116 seconds 

2 examples, 1 failure 


그래서 예상치 못한 비트는 같은 정규 인스턴스가 여러 사양에 걸쳐 사용 된 것으로 보인다는 것이다. 이 특별한 경우에 @start_time이 첫 번째 사양의 값으로 초기화되어 두 번째 사양의 잘못된 오류를 일으 킵니다.

답변

관련 문제