使用Rspec进行测试 - 正确的方法

时间:2014-09-05 14:13:01

标签: ruby testing rspec tdd

我在编码方面最薄弱的地方是使用TDD& amp; BDD方法 - 我倾向于只编写代码..但这是我正在尝试的东西。

有人能指出解决以下问题的最佳方法:

的Class1:

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?可能不遵守单一责任原则,我可能会将其移到自己的模块/类中,我可以使用存根进行测试。

如果有人能为我阐明这一点,那就太棒了。

编辑:

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 个答案:

答案 0 :(得分:1)

你的问题让我感到困惑,这就是你要问的问题

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

然后测试

 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

目前您的测试没有任何期望,所以我添加了一个期望myclass respiond_到react_upon?方法,另一个期望myclass.process(xml)回复String等于yeah

相关问题