2012-02-27 4 views
1

私はRSpec(2.8.0)カスタムマッチャー機能で直感的なやり方を経験しましたが、バグか機能か混乱しているのだろうかと思います。コードを見てみましょう:RSpecカスタムマッチャーは仕様間の状態を保ちます

# 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が第1の仕様からの値で初期化され、第2の仕様の誤った失敗を引き起こす。

答えて

関連する問題