반응형
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-counts 의 Receive Counts에서
희망 =)
expect
rspec 의 새로운 구문은 다음과 같습니다.
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
반응형
'Programing' 카테고리의 다른 글
C ++ 11에서 thread_local은 무엇을 의미합니까? (0) | 2020.08.13 |
---|---|
Pandas 데이터 프레임은 각 그룹의 첫 번째 행을 가져옵니다. (0) | 2020.08.13 |
할당 및 동등성 검사가있는이 if 문이 거짓으로 평가되는 이유는 무엇입니까? (0) | 2020.08.13 |
vim과 함께 git commit -a 사용 (0) | 2020.08.13 |
Oracle SID 및 데이터베이스 이름 확인 (0) | 2020.08.13 |