2016-09-18 16 views
0

私はRSpecを学ぶための探求をしています。現在、私はbuilt-in matchersを勉強しています。RSpec kind_of?戻り値が間違っています

私はrelishapp siteオンexpect(actual).to be_kind_of(expected)

に少し混乱しています、それは

obj.should be_kind_of(タイプ)であることをbe_kind_ofの振る舞いを言う:obj.kind_of呼び出します(タイプ)、これは? typeがobjのクラス階層にあるか、またはモジュールであり、objのクラス階層のクラスに含まれている場合はtrueを返します。

APIdockがthis exampleを述べている:

module M; end 
class A 
    include M 
end 
class B < A; end 
class C < B; end 

b.kind_of? A  #=> true 
b.kind_of? B  #=> true 
b.kind_of? C  #=> false 
b.kind_of? M  #=> true 

私はRSpecの上でそれをテストしたときに私はしかし、それはfalseを返します。例を言うとき

module M; end 
class A 
    include M 
end 
class B < A; end 
class C < B; end 

describe "RSpec expectation" do 
    context "comparisons" do 
    let(:b) {B.new} 

    it "test types/classes/response" do 
     expect(b).to be kind_of?(A) 
     expect(b).to_not be_instance_of(A) 
    end 
    end 
end 


1) RSpec expectation comparisons test types/classes/response 
    Failure/Error: expect(b).to be kind_of?(A) 

     expected false 
      got #<B:70361555406320> => #<B:0x007ffca7081be0> 

はなぜRSpecのは、falseを返すんそれはtrueを返す必要がありますか?

答えて

0

あなたは2種類のマッチャー、should and expectを混在させています。 rspec-expectationsのためのマニュアルを参照してください:あなたはuse both、またはそれらのいずれかを選択しなければならない

expect(actual).to be_an_instance_of(expected) # passes if actual.class == expected 
expect(actual).to be_a(expected)    # passes if actual.kind_of?(expected) 
expect(actual).to be_an(expected)    # an alias for be_a 
expect(actual).to be_a_kind_of(expected)  # another alias 

1

あなたは

expect(b).to be kind_of?(A) 

を書いているが、マッチャが

expect(b).to be_kind_of(A) 

では、アンダースコアと疑問符の欠如に注意してください。あなたは、整合となりますようあなたはRSpecのテストそのものではないb#kind_of?を呼んでいる

b.equal?(kind_of?(A)) 

場合は、書いた テストは合格します。

関連する問題