2015-02-06 2 views
0

나는 예상치 못한 실패로 작동하고있는 테스트를 가지고 있습니다. ==가 double에 두 번 호출된다고합니다. 그것은 그 방법에 대한 논쟁이기도하니? 우리는 rspec 이중 수신 이유 : == 두 번

내가 약

require 'rspec' 
describe 'rspec test doubles' do 
    let(:a_double) { double('a_double') } 

    it 'should only call == once' do 
     expect(a_double).to receive(:==).and_return(true) 
     a_double == a_double 
    end 
end 

을 말하고 무엇을 증류 한 예입니다 그리고 내가 rspec-mocksTestDouble 클래스를 보면이 시험을

F 

Failures: 

    1) rspec test doubles should only call == once 
    Failure/Error: expect(watir_driver).to receive(:==).and_return(true) 
     (Double "watir_driver").==(*(any args)) 
      expected: 1 time with any arguments 
      received: 2 times with any arguments 
    # ./double_spec.rb:7:in `block (2 levels) in <top (required)>' 

Finished in 0.019 seconds (files took 0.26902 seconds to load) 
1 example, 1 failure 

Failed examples: 

rspec ./double_spec.rb:6 # rspec test doubles should only call == once 

답변

1

를 실행할 때이 내가 무엇을 얻을 이것을 찾으십시오 :

# This allows for comparing the mock to other objects that proxy such as 
# ActiveRecords belongs_to proxy objects. By making the other object run 
# the comparison, we're sure the call gets delegated to the proxy 
# target. 
def ==(other) 
    other == __mock_proxy 
end 

그래서 rspec purposefu처럼 보입니다. lly 전화를 거절합니다. 두 개의 별도의 복식 있었다 척,이 라인을 따라 뭔가 할 것 double_1 == double2 일 :

  • 전화 double_1 == double2
  • 우리가 보았 듯이,이 다음 역 것이지만,이 double_1의 프록시 double_1을 교환 할 것 : double2 == __mock_proxy
  • double2 그러면 otherdouble_1의 프록시 인 other == __mock_proxy이 다시 호출됩니다. other 대신 TestDoubleProxy 객체이기 때문에
  • 는 다른 == 방법은 (이 시점에서 두 Proxy 객체를 비교하는)라고하며, 우리는 마침내 비교를 얻을.

실제로 볼 수 있듯이 ==은 실제로 첫 번째 이중, 두 번째 이중 및 마지막으로 첫 번째 이중의 프록시라는 세 가지 개별 항목에 대해 호출됩니다. 첫 번째와 두 번째 더블은 같기 때문에 두 번 호출됩니다. 그들이 그런 식으로 행동을 시작했을 때

당신은 알고 계십니까 TestDouble's == method

+0

Proxy's == method를 참조하십시오? rspec 3.0처럼 보이지 않았다 –

+0

@DaneAndersen 잘 모르겠습니다. 나는 3.0에 없었던 것에 놀랐다 -'=='의 정의가 거의 3 년 동안 계속되었고, 3.0을위한 코드를 보면 여전히'other == __mock_proxy' 일을한다. –

관련 문제