2016-11-13 8 views
2

의 출력을 테스트하므로 클래스 Weather가 있으며 임의의 결과가 반환되어야하며 스텁을 사용하여 테스트하고 싶습니다. 이 글에서 마틴 플라워 (Martin Flower)에 대한 기사를 읽었습니다. http://www.martinfowler.com/articles/mocksArentStubs.html 그리고 나는 이것이 가장 쉬운 해결책이라고 생각합니다. 그러나 구문의 예를 찾는 것은 어렵습니다. 시험의 예를 들어 주시겠습니까? 이것은 내 숙제의 일부입니다.rspec의 스텁을 사용하여 rand

class Weather 

    def conditions 
    return :good if chance > 0.3 
    :stormy 
    end 

    def chance 
    rand 
    end 

end 

답변

2

예제에 따르면 구현이 아닌 chance의 동작을 테스트하려고합니다.

describe Weather do 
    it 'returns good' do 
    weather = Weather.new 
    allow(weather).to receive(:chance).and_return(0.8) 
    expect(weather.conditions).to eq :good 
    end 
end 
+0

cool. 멋지고 단순한 – Agata