【问题标题】:Testing with Rspec - The correct way使用 Rspec 进行测试 - 正确的方法
【发布时间】:2014-09-05 14:13:00
【问题描述】:

在编码方面,我最弱的一点是使用 TDD 和 BDD 方法 - 我倾向于只编写代码.. 但这是我正在努力的事情。

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

第一类:

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

【问题讨论】:

    标签: ruby testing rspec tdd


    【解决方案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_to react_upon? 方法和另一个期望 myclass.process(xml) 以等于 StringString 响应。

    【讨论】:

    • 对不起,它想把我脑子里的东西弄出来!我要说的是,您期望的是,测试整个类(包括 react_upon? 方法)是我需要测试它的方式吗?还是我测试react_upon?单独期望中的方法,以确保它正在做我需要它做的事情?
    • @Vade 我不知道 react_upon 是什么?方法看起来像,但我现在也添加了一个期望。
    • 就是这个!我用错了电话。我正在使用 expect(myclass).to have_recieved(:react_upon?) 谢谢!
    • @Vade 我也刚刚添加了 2 个期望来直接测试实际的 react_upon? 方法。
    • @Vade 不确定这是否是您要问的,但我添加了一个规范测试来测试您的班级 received 使用 xmlreact_upon? 的调用,您可以将其更改为 @ 987654332@ 或receive(:react_upon?).with(anything)。任何事情都意味着我不在乎我传递给它的东西,只关心它被调用了。
    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 2023-03-18
    • 2013-04-08
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多