Programing

RSpec에서 "should_receive"를 더 많이 말하는 방법

lottogame 2020. 8. 13. 07:38
반응형

RSpec에서 "should_receive"를 더 많이 말하는 방법


내 테스트에서 이걸 가지고

Project.should_receive(:find).with(@project).and_return(@project)

하지만 객체가 해당 메서드 호출을 두 번 받으면

Project.should_receive(:find).with(@project).and_return(@project)
Project.should_receive(:find).with(@project).and_return(@project)

다음과 같이 말하는 방법이 있습니까?

Project.should_receive(:find).with(@project).and_return(@project).times(2)

이것은 구식입니다. 아래 Uri의 답변을 확인하십시오

2 회 :

Project.should_receive(:find).twice.with(@project).and_return(@project)

정확히 n 번 :

Project.should_receive(:find).exactly(n).times.with(@project).and_return(@project)

n 번 이상 :

Project.should_receive(:msg).at_least(n).times.with(@project).and_return(@project)

자세한 내용은 https://www.relishapp.com/rspec/rspec-mocks/v/2-13/docs/message-expectations/receive-countsReceive Counts에서

희망 =)


expectrspec 의 새로운 구문은 다음과 같습니다.

2 회 :

expect(Project).to receive(:find).twice.with(@project).and_return(@project)

정확히 n 번 :

expect(Project).to receive(:find).exactly(n).times.with(@project).and_return(@project)

n 번 이상 :

expect(Project).to receive(:msg).at_least(n).times.with(@project).and_return(@project)

@JaredBeck이 지적했습니다. 이 솔루션은 any_instance통화 중에 작동하지 않았습니다 .

어떤 경우에도 should_receive 대신 스텁을 사용했습니다.

Project.any_instance.stub(:some_method).and_return("value")

이것은 어떤 경우에도 작동합니다. 그래도 시간.


should_receive와 반대로은 any_instance클래스가 지정된 횟수만큼 메시지를 수신 할 것으로 예상합니다.

any_instance 반면에 일반적으로 메서드를 스터 빙하는 데 사용됩니다.

그래서 첫 번째 경우는 우리가 테스트하고 싶은 기대이고, 두 번째 경우는 우리가 계속 진행할 수 있도록 다음 줄로 메소드를 지나가는 것입니다.

참고 URL : https://stackoverflow.com/questions/1328277/how-to-say-should-receive-more-times-in-rspec

반응형