2016-09-18 2 views
0

나는 RSpec을 배우기 위해 노력 중입니다. 현재 built-in matchers을 공부하고 있습니다. RSpec kind? 잘못된 결과를 반환합니다

내가 relishapp siteexpect(actual).to be_kind_of(expected)

에 조금 혼란 스러워요, 그것은 be_kind_of의 행동이

obj.should be_kind_of (유형)로 말한다 : obj.kind_of 호출 (유형), 어떤 type이 obj의 클래스 계층에 있거나 모듈이고 obj의 클래스 계층에있는 클래스에 포함되어 있으면 true를 반환합니다.

APIdock 상태 this example : 나는 RSpec에 그것을 시험 때

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 

, 그것은 내가 할 때 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

두 종류의 matcher가 있습니다. 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)) 

경우에 당신이 쓴 테스트를 통과합니다.

관련 문제