2014-04-28 3 views
0

나는 시험 블록이 있습니다리팩토링 테스트는

describe 'without' do 

    describe 'author' do 
    let(:author) {nil} 
    it('fails') {assert_raises(ArgumentError) { excerpt }} 
    end 

    describe 'title' do 
    # same content as above, but testing title 
    end 

    describe 'content' do 
    # same content as above, but testing content 
    end 

end 

이 잘 작동하고 테스트를 통과 -하지만 많은 반복이 여기있어 이후로, 내가 리팩토링 원 :

describe 'without' do 

    describe 'author' do 
    let(:author) {nil} 
    it('fails') {assert_failure(excerpt} # added a method in this line 
    end 

    describe 'title' do 
    # same content as above, but testing title 
    end 

    describe 'content' do 
    # same content as above, but testing content 
    end 

    # and the method here 
    def assert_failure(instance) 
    assert_raises(ArgumentError) { instance } 
    end 

end 

그러나이 작동하지 않습니다 - 내 테스트는 다음 오류와 함께 실패합니다.

construction::without::author#test_0001_fails: 
ArgumentError: Excerpt cannot be built: Author missing 

내가 예상 한 오류는 어느 것입니까? 누락 된 정보로 생겨 났으며 정확하게 테스트하고 있습니다. 따라서이 어설 션을 추출 할 때 ArgumentError이 제기되어서 assert_raises이 모든 종류의 비교를 수행 할 수 있기 전에 테스트를 중지하는 것으로 보입니다. 나는이 어설 션을 위해 code을 조사했지만 코드가 다른 방법으로 추출 될 때 이것이 일어날 이유를 알 수는 없다.

답변

1

는 다음과 같은 구문을 쓸 때 :

assert_raises(ArgumentError) { excerpt } 

는 당신이 실제로 assert_raises에 전달하는 것은 block입니다. 즉 코드 (excerpt)는 메소드 자체에서 평가할 때까지 평가되지 않습니다. 비록이 구문

:

assert_failure(excerpt) 

excerptassert_failure

가 시작되기 전에 을 평가한다.

def assert_failure(&block) 
    assert_raises(ArgumentError, &block) 
end 

    describe 'author' do 
    let(:author) {nil} 
    it('fails') { assert_failure { excerpt } } 
    end 
+0

좋은 답변, 감사 : 위의 동작을 복제 할 경우

, 당신은 assert_failure에게 블록을 통과해야! – dax