我正在使用rspec-mock进行测试驱动开发。我开始实现单个类并使用rspec-mock模拟/stub其他类。尚未实现的类的模拟对象效果很好。但是,当我尝试模拟一个尚不存在的类的类方法时,我没有成功。我的类“哈希”应该有一个类方法“calculate_hashes”接收文件名并返回哈希。我试过了allow(Hashes).toreceive(:calculate_hash)do|file|#looksupwhattoreturnend给出错误“Hashesisnotaclass”。然后我实现了一个类“Hashes”classHashesend然后只尝试以相同的方式对类方法进行stub
有什么方法可以使用Minitest::Mock验证从未调用过或仅调用过多次的方法提前致谢 最佳答案 具体次数:Minitest::Mock强制开发人员明确消息预期,因此如果您希望给定方法被调用x次,您需要非常直白。my_mock=Minitest::Mock.newx.times{my_mock.expect:some_method,:return_val}从未调用过:从哲学上讲,Minitest避免测试没有发生的事情。虽然不完全相似,但请查看thispost或谷歌“minitestassertnothingraised”。Mini
Variants这个问题已被多次问到,但大多数都与摩卡有关,而我没有使用它。我是Rails的新手,所以这看起来微不足道,但我无法在我的规范文件中使用mock(用于名为competitions的Controller)。require'rails_helper'require'spec_helper'describeCompetitionsControllerdobefore:eachdo@fake_c=mock(Competition,:competition_id=>1,:competition_name=>'one',:competition_des=>'any')enddescri
我有一个这样的测试用例:describeWorkCardsControllerdoit"something"dowork_card=instance_double(WorkCard,{:started?=>true})#somemorecodeendend当我运行RSpec时,出现错误:undefinedmethod'instance_double'for#根据http://rubydoc.info/github/rspec/rspec-mocks/RSpec/Mocks/ExampleMethods这种方法存在。所以我尝试通过以下方式直接访问它:describeWorkCardsCo
我使用的是带有Ruby1.9.3的minitest版本如何使用它测试模拟的多次调用?我需要类似的东西mockObject.expect.times(2):method,[return_1firsttime,return_2secondtime]mockObject.verify有什么办法可以实现吗? 最佳答案 您需要调用expect每次调用该方法。mockObject.expect:method,return_1,[first,time,args]mockObject.expect:method,return_2,[second,t
我可以测试参数是否传递如下:RSpec.describedoitdoobj=doubleexpect(obj).toreceive(:method).with(1,2,3)obj.method(1,2,3)endend我应该如何处理block参数?我理想中的代码:RSpec.describedoitdoobj=doubleproc=Proc.new{}expect(obj).toreceive(:method).with(1,2,3).with_block(proc)obj.method(1,2,3,&proc)endend 最佳答案
我有一个关于如何在示例之间共享rspec-mocks的double的问题。我正在使用rspec-mocks3.1.3编写一个新的Rails应用程序。我习惯于使用旧的(我有一个模型方法:defself.from_strava(activity_id,race_id,user)@client||=Strava::Api::V3::Client.new(access_token:'abc123')activity=@client.retrieve_an_activity(activity_id)result_details={race_id:race_id,user:user}result_
是否有与Rspec的“mock().as_null_object”等效的Mocha? 最佳答案 是的。使用“stub_everything()”记录在此处:http://mocha.rubyforge.org/classes/Mocha/API.html#M000004. 关于ruby-是否有与Rspec的“mock().as_null_object”等效的Mocha?,我们在StackOverflow上找到一个类似的问题: https://stackover
Rspec-mocks具有expect(some_object).toreceive(:some_method).and_call_original。我可以用Mocha做这个吗?如果可以,怎么做?some_object.expects(:some_method).......什么? 最佳答案 我相当确定没有办法做到这一点。浏览sourcecode,有评论提到完全替换了原来的方法。#Theoriginalimplementationofthemethodisreplacedduringthetestandthenrestoredatt
以下代码引发错误:undefinedmethod'any_instance'forString:Classrequire'rspec'RSpec.configuredo|config|config.mock_with:rspecenddescribeStringdoit'stubs'doString.any_instance.stub(:foo).and_return(1)''.foo.shouldeq(1)endend如何将Mocks模块包含到类或对象类中? 最佳答案 any_instance最近被添加到rspec,所以你的例子现