我有一个类和一个规范。classStoredefactivate(product_klass,product_id)product=product_klass.find(product_id)ifproduct.inactive?product.update_attribute:active,trueendendenddescribeStoredoit"shouldactivateaninactiveproduct"doproduct=mockproduct.stub(:inactive?).and_return(true)store=Store.newstore.activate(2
我尝试使用工厂构建json,但是当我尝试构建build时它是空的。下面是Factory类。require'faker'FactoryGirl.definedofactory:accountdo|f|f.name{Faker::Name.name}f.description{Faker::Name.description}endfactory:accjson,class:Hashdo"@type""accountResource""createdAt""2014-08-07T14:31:58""createdBy""2""disabled""false"endend下面是我正在尝试构建的
前一段时间我问了“HowtotestobtainingalistoffileswithinadirectoryusingRSpec?”,虽然我得到了一些有用的答案,但我仍然卡住了,因此提出了一个新问题,其中包含有关我正在尝试做的事情的更多细节。我正在编写我的第一个RubyGem。它有一个模块,其中包含一个类方法,该方法返回一个数组,该数组包含指定目录中的非隐藏文件列表。像这样:files=Foo.bar:directory=>'./public'该数组还包含一个表示文件元数据的元素。这实际上是从文件内容生成的哈希值的哈希值,其想法是即使更改单个文件也会更改哈希值。我已经编写了待定的RS
我知道以下事情有效:返回一个参数subject.should_receive(:get_user_choice){|choices|choices.to_a[0]}和一个序列(它将在第一次调用时返回0,第二次“退出”)subject.should_receive(:get_user_choice).and_return(0,"exit")但是如何组合它们呢?如果我想第一次返回参数然后返回“exit”怎么办 最佳答案 或者:subject.should_receive(:get_user_choice).ordered.and_ret
我正在使用puts命令在我的应用程序中打印一些自定义消息。但是,我不希望这些出现在我的测试输出中。所以,我尝试了一种方法来stubputs,如下所示。但它仍然输出我的信息。我做错了什么?stubs(:puts).returns("")#DidnotworkoutObject.stubs(:puts).returns("")#Didnotworkouteitherputs.stubs.returns""#NotworkingaswellKernel.stubs(:puts).returns""#Noluck我正在使用Test::Unit 最佳答案
我在String类中有一个调用Array实例方法“shuffle”的实例方法。为了让我使用RSpec测试该方法,我想对其进行stub。vowels=%w(aeiouy)vowels.shuffle我试过:Array.stub(:shuffle).and_return(%w(aeiouy))[].stub(:shuffle).and_return(%w(aeiouy))但都没有用:(我在这里遗漏了一些东西?也许是mock?但是我应该mock什么......谢谢。 最佳答案 使用any_instance。例如:describe"any_
我如何使用rspecstub接受两个用户输入的方法?可能吗?classMirrordefechoarr=[]print"entersomething:"arr[0]=gets.chompprint"entersomething:"arr[1]=gets.chompreturnarrendenddescribeMirrordoit"shouldecho"do@mirror=Mirror.new@mirror.stub!(:gets){"foo\n"}@mirror.stub!(:gets){"bar\n"}arr=@mirror.echo#@mirror.should_receive(:
我知道有一种方法可以让stub像这样返回多个不同的值:subject.stub(:method_call.and_return(1,2,3)但我希望这样的事情是可能的:subject.stub(:method_call).and_raise(Exception).oncesubject.stub(:method_call).and_return(1)但我还没有找到一种优雅的方式让stub仅在第一次调用时引发异常。有什么建议吗? 最佳答案 我知道的唯一方法是使用这样的计数器变量:counter=0times=2TestModel.an
我的类(class)结构如下:classAbcONE_CLASS_LEVEL_CONSTANT_BEING_READ_FROM_DB=GloablAttributeValue.read_from_dbdefsome_method_that_use_above_constant#thisfunctionbehavesdifferentlyfordifferentvaluesofONE_CLASS_LEVEL_CONSTANT_BEING_READ_FROM_DBendend现在我想根据不同的值ONE_CLASS_LEVEL_CONSTANT_BEING_READ_FROM_DB对some
我想在引发异常的情况下对HTTParty的前两次调用stub,然后,第三次调用应该返回值。beforedoallow(HTTParty).toreceive(:get).exactly(2).times.with(url).and_raise(HTTParty::Error)allow(HTTParty).toreceive(:get).with(url).and_return('{}')end但一个允许覆盖另一个。如何设置stub以在前几次尝试时引发错误,然后让它返回一个值? 最佳答案 根据thisgithubissue中提供的信