Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 使用Rspec进行测试-正确的方法_Ruby_Testing_Rspec_Tdd - Fatal编程技术网

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

Ruby 使用Rspec进行测试-正确的方法,ruby,testing,rspec,tdd,Ruby,Testing,Rspec,Tdd,说到编码,我最薄弱的一点是使用TDD和BDD方法——我倾向于只编写代码。。但这是我正在努力解决的问题 有人能指出解决以下问题的最佳方法吗: 类别1: module TempMod class MyClass def initalize(config) @config = config end def process(xml) if react_upon? xml.something

说到编码,我最薄弱的一点是使用TDD和BDD方法——我倾向于只编写代码。。但这是我正在努力解决的问题

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

类别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
描述TempMod::MyClass do
let(:config){double}
let(:myclass){TempMod::myclass.new config}
上下文“假设xml是我们响应的对象”,那么
它“应该检查它是否有效”执行以下操作
myclass.process“此处”
结束
它“应该输出yea”
结束
结束
如何测试它是否正在调用react_?方法。我想看看它在叫它吗

测试它的正确方法是,测试所有的功能,比如react_on?它本身独立于其他功能吗

这正是让我对这种测试感到困惑的主要原因。我是在测试整个类,还是只是单独测试函数,而不是测试它们与该类中其他函数的交互

我也意识到你的反应?可能不遵守单一责任原则,我可能会将其转移到自己的模块/类中,我可以使用存根进行测试

如果有人能帮我解释一下,那就太棒了

编辑:

描述TempMod::MyClass do
let(:有效的\u计划\u状态\u xml){
“2329建议确认”
}
let(:config){double}
let(:status_resolver){double}
subject(:message_processor){TempMod::MyClass.new config,status_resolver}
上下文“如果消息XML有效”,请执行以下操作
它“应该检查消息的上下文”do
期望(消息\u processor.process valid\u planning\u status\u xml)。调用:check\u me
结束
上下文“如果消息用于计划事件更新”,请执行
它“应该叫某事”做
悬而未决的
结束
结束
上下文“如果消息用于录制作业更新”,请执行以下操作
结束
上下文“如果消息用于视频标题更新”,请执行以下操作
结束
结束
结束

你的问题让我有点困惑,这就是你要问的吗

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
描述TempMod::MyClass do
let(:config){double}
let(:myclass){TempMod::myclass.new config}
上下文“假设xml是我们响应的对象”,那么
它“应该作出反应?”你认为呢
期望(myclass).响应(:react\u on?)
结束
它“应该对有效的xml作出反应”吗
期望(myclass.react_on?(此处是您的有效反应))为真
结束
它“不应该对无效的xml作出反应”吗
期望(myclass.react_on?(此处显示您的无效反应))为假
结束
它“应该说‘是’,如果它是有效的”做
expect(myclass.process('here'))到eq('yeah'))
结束
它“如果无效,应该说‘不’”do
expect(myclass.process('here'))到eq('nah'))
结束
它“应该检查消息的上下文”do
期望(myclass).接收(:react_on?)和('here')
myclass.process('here')
结束
结束
结束

现在您的测试没有任何期望,所以我添加了一个期望
myclass
响应
react\u on?
方法的测试,以及另一个期望
myclass.process(xml)
响应
字符串的测试,该字符串等于

对不起,它试图把我脑子里想出来的东西拿出来!我想说的是,你所期望的是,测试整个类(包括react_on?方法),这就是我需要测试它的方式吗?或者我要测试你的反应?方法,以确保它正在做我需要它做的事情?@Vade我不知道它会做出什么反应?方法看起来像,但我现在也添加了一个期望值。就是这个!我打错电话了。我正在使用expect(myclass)。为了让您收到(:react\u on?)谢谢@Vade我还添加了两个期望值来直接测试实际的
react\u on?
方法。@Vade不确定这是否是您要求的,但我添加了一个spec测试来测试您的类
是否收到了
react\u on?
调用
xml
您可以将其更改为
receive(:react\u on?)一次
接收(:作出反应?)。用(任何东西)
。任何事都意味着我不在乎我传递给它的是什么,只在乎它的名字。
 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