본문 바로가기

소프트웨어-이야기/테스트-자동화

[Rspec]테스트 할 코드에 원하는 메소드가 실행되고 있는지 확인하기



should_receive(:확인할 메소드명).with(넘겨질 파라미터 값).and_return({반환되어야하는 값})

: Ruby Rspec에서 테스트 코드에서 검증하고자 하는 클래스에서 *** 메소드가 실행되고 있는지 확인하는 기능을 지원한다.


예시 

it "calls Zoogle.graveyard_locator" do

Zoogle.should_receive(:graveyard_locator).with(zombie.graveyard).and_return({latitude: 2, longitude: 3})
zombie.geolocate

end



옵션주기

should_receive(:function)에는 옵션을 줄 수 있다.

should_receive(:function).once : 한번 호출되었다.

should_receive(:function).at_least : 적어도 두번은 호출 되었다.

should_receive(:function).with(/jimin/) : with에 넘어갈 파라미터 값에 정규표현식을 반영할 수있다.

should_receive(:function).with(3, kind_of(Numeric)) : 파라미터로 넘어오는 객체가 *** 클래스를 상속받았는지 체크할 수 있다. 


tobe.. 아래의 페이지 보면서 stub / mock 의 차이 정리하기 

http://www.agileventures.org/articles/testing-with-rspec-stubs-mocks-factories-what-to-choose


#codeschool Rspec