Tag: rr

Rubyunit testing技术,Mocking和Stubbing

我被招募为SW Dev,我正在尝试使用RSPEC和RR进行unit testing,但是很难决定特定策略,主要是因为我被分配给已经编码的unit testing书面。 考虑以下代码,它是一个名为method1的大方法的一部分: if ([“5234541252”, “6236253223”].include?(self.id)) self.DoCheck logs.add ‘Doing check’, “id = #{self.id}” return end 这部分方法的相关unit testing将是这样的: “should do check only if id = 5234541252 or 6236253223” 但我遇到了几个问题,基本上涉及最佳实践,例如: 如何使用RR和RSPEC检查是否已从“method1”中调用DoCheck? 我尝试过使用dont_allow(Object).DoCheck但是它不起作用。 describe :do_route do it “should do check only if id = 5234541252 or 6236253223” do user = Factory(:User) user.id = “5234541252” dont_allow(user).DoCheck user.method1 […]

使用rr得到块

我正在尝试使用rr测试以下代码: response = RestClient.get(url, {:params => params}){|response, request, result| response } 在vanilla rspec ,你会做这样的事情: RestClient.should_receive(:get).with(url, {:params => params}).and_yield(response, request, result) 我怎么用rr做同样的事情? 建立: let(:url) { “http://localhost/” } let(:params) { {:item_id => 1234, :n => 5} } let(:response) { Object.new } let(:request) { Object.new } let(:result) { Object.new } 我尝试了很多变化: mock(RestClient).get(url, {:params => params}) { response, request, […]

unit testingRuby块通过模拟与rr(是flexmock)

我如何对以下单元进行unit testing: def update_config store = YAML::Store.new(‘config.yaml’) store.transaction do store[‘A’] = ‘a’ end end 这是我的开始: def test_yaml_store mock_store = flexmock(‘store’) mock_store .should_receive(:transaction) .once flexmock(YAML::Store).should_receive(:new).returns(mock_store) update_config() end 如何测试块内部的内容? 更新 我已将我的测试转换为规范并切换到rr模拟框架: describe ‘update_config’ do it ‘calls transaction’ do stub(YAML::Store).new do |store| mock(store).transaction end update_config end end 这将测试调用的事务。 如何在块内测试: store[‘A’] = ‘a’ ?

在测试“无限循环”时,最佳做法是什么?

我的基本逻辑是在某处运行无限循环并尽可能地测试它。 拥有无限循环的原因并不重要(游戏的主循环,类似守护进程的逻辑……)而且我更多地询问有关这种情况的最佳实践。 我们以此代码为例: module Blah extend self def run some_initializer_method loop do some_other_method yet_another_method end end end 我想使用Blah.run测试方法Blah.run (我也使用RR ,但普通的rspec是一个可接受的答案)。 我认为最好的方法是分解更多,比如将循环分成另一种方法或其他方法: module Blah extend self def run some_initializer_method do_some_looping end def do_some_looping loop do some_other_method yet_another_method end end end …这允许我们测试run并模拟循环…但是在某些时候需要测试循环内的代码。 那么在这种情况下你会做什么? 只是不测试这个逻辑,意味着测试some_other_method & yet_another_method但不测试do_some_looping ? 通过模拟在某个时刻让循环中断? ……别的什么?