2014-09-05 2 views
-1

코딩에있어 가장 약한 점은 TDD & BDD 방법을 사용하는 것입니다. 코드를 작성하는 경향이 있습니다. 그러나이 코드는 제가 작업하려고하는 것입니다.Rspec으로 테스트하기 - 올바른 방법

사람이 다음과 같은 문제에 대해 갈 수있는 가장 좋은 방법을 지적 할 수 :

클래스 1 :

module TempMod 
    class MyClass 

     def initalize(config) 
      @config = config 
     end 

     def process(xml) 
      if react_upon? xml.something 
       puts 'yeah' 
      else 
       puts 'nah' 
      end 
     end 

     def react_upon?(xml_code) 
      #code here 
     end 

    end 
end 

그래서이 클래스를 테스트하거나보기의 TDD 지점에서 구축하고 싶었 말할 수 그래서 나는 내 시험을 쓴다 :

describe TempMod::MyClass do 

    let(:config) {double} 
    let(:myclass) {TempMod::MyClass.new config} 

    context 'Given that the xml is something we react upon' do 
     it 'should check that it is valid' do 
      myclass.process '<some><xml>here</xml></some>' 
     end 
     it 'should output yea' 
    end 
end 

나는 그것이 react_upon을 호출하고 있는지 어떻게 테스트 할 수 있는가? 방법. 나는 그것이 그것을 부르는 것을보고 싶습니까?

react_upon과 같은 모든 기능을 테스트하려면 테스트하는 적절한 방법이 있습니까? 그 자체로 다른 기능들과 독립적으로?

이것은 테스트와 관련하여 가장 혼란스러운 부분입니다. 전체 클래스를 테스트하거나 해당 클래스의 다른 함수와의 상호 작용이 아닌 함수를 개별적으로 테스트하고 있습니까?

또한 react_upon을 알고 있습니까? Single 책임 원칙을 따르지 않을 수도 있으며 아마도 스텁을 사용하여 테스트 할 수있는 자체 모듈/클래스로 옮길 것입니다.

누구나 나를 위해 이것에 대해 밝힐 수 있다면 그것은 굉장 할 것입니다.

편집 :

describe TempMod::MyClass do 

    let (:valid_planning_status_xml) { 
    '<StatusUpdate> <TitleId>2329</TitleId> <FromStatus>Proposed</FromStatus> <ToStatus>Confirmed</ToStatus> </StatusUpdate>' 
    } 

    let(:config) { double } 

    let(:status_resolver) { double } 

    subject(:message_processor) { TempMod::MyClass.new config, status_resolver } 

    context 'Given that the message XML is valid' do 

    it 'should check the context of the message' do 
     expect(message_processor.process valid_planning_status_xml).to call :check_me 
    end 

    context 'Given that the message is for a planning event update' do 

     it 'should call something' do 
      pending 
     end 
    end 

    context 'Given that the message is for a recording job update' do 
    end 

    context 'Given that the message is for a video title update' do 
    end 
    end 
end 

답변

1

귀하의 질문은 조금 당신의 검사 결과가 그래서 기대가 없습니다 당신이 그런 지금

describe TempMod::MyClass do 

    let(:config) {double} 
    let(:myclass) {TempMod::MyClass.new config} 

    context 'Given that the xml is something we react upon' do 
    it "should respond to react_upon?" do 
     expect(myclass).to respond_to(:react_upon?) 
    end 
    it "should react_upon? valid xml" do 
     expect(myclass.react_upon?(YOUR VALID REACTION GOES HERE)).to be_true 
    end 
    it "should not react_upon? invalid xml" do 
     expect(myclass.react_upon?(YOUR INVALID REACTION GOES HERE)).to be_false 
    end 
    it "should say 'yeah' if it is valid" do 
     expect(myclass.process('<some><xml>here</xml></some>')).to eq('yeah') 
    end 
    it "should say 'nah' if it is invalid" do 
     expect(myclass.process('<some><xml>here</some>')).to eq('nah') 
    end 
    it 'should check the context of the message' do 
     expect(myclass).to receive(:react_upon?).with('<some><xml>here</xml></some>') 
     myclass.process('<some><xml>here</xml></some>') 
    end 
    end 
end 

같은 테스트

module TempMod 
    class MyClass 
    def initalize(config) 
     @config = config 
    end 
    def process(xml) 
     react_upon?(xml.something) ? 'yeah' : 'nah' 
    end 
    def react_upon?(xml_code) 
     #code here 
    end 
    end 
end 

을 요구하는이 나를 혼란 나는 react_upon? 방법에 respiond_to에 myclass를 기대하는 것을 추가했습니다. myclass.process(xml)String 인 응답을 yeah으로 응답 할 것으로 예상하는 다른 서버가 있습니다.

+0

죄송합니다. 그게 내 머리에 뭐가 나오려고하는지! 내가 들으려는 요점은 당신이 넣는 기대와 함께, 그것은 전체 클래스를 테스트하는 것입니다 (react_upon? 메소드 포함) 제가 테스트 할 필요가있는 방법입니까? 또는 react_upon을 테스트합니까? 방법은 별도의 기대에, 그것이 내가 할 일을하고 있는지 확인하기 위해? – Vade

+1

@ 베이드 나는 react_upon 무엇을 몰라? 방법은 비슷해 보이지만 나는 그것에 대한 기대도 덧붙였다. – engineersmnky

+0

그 중 하나! 나는 잘못된 전화를 사용했다. 나는 expect (myclass)를 사용했다. have_recieved (: react_upon?) 고마워! – Vade

관련 문제