특정 오류가 발생하는 모델에서 기능을 테스트하고 싶습니다. 이 기능은 다음과 같은 : Rails ActiveSupport : 오류가 발생했다고 주장하는 방법은 무엇입니까?
def merge(release_to_delete)
raise "Can't merge a release with itself!" if(self.id == release_to_delete.id)
raise "Can only merge releases by the same artist" if(self.artist != release_to_delete.artist )
#actual merge code here
end
가 지금은 어설 션을 수행 할 것을 나는 예외가 실제로 던져지는 것으로, 그 예외의 각을 일으키는 매개 변수를 사용하여이 함수를 호출 할 때. ActiveSupport 설명서를보고 있었지만 유망한 것을 찾지 못했습니다. 어떤 아이디어?
고마워요! 그래도 예외 대신 "RuntimeError"를 사용해야했습니다. assert_raise {artist1.merge (artist1)} 이 가이드는 매우 도움이되었습니다. http://guides.rubyonrails.org/testing.html – spilliton
@spilliton : Ruby 인터프리터는 위의 코드를'assert_raise (Exception() {whatever.merge}) '로 취급하기 때문에, 즉 주어진 블록으로 호출되는'Exception' 메서드를 찾습니다. 괄호를 추가하면 그 모호성이 해결됩니다. (이미 답변을 고쳤습니다) –
rspec 3에서'expect {whatever.merge} .to raise_error' (http://www.relishapp.com/rspec/rspec-expectations/v/3-1/docs/built- in-matcher/raise-error-matcher) –