我知道以下事情有效:返回一个参数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
我是Rails和测试模型的新手。我的模型类是这样的:classTester而且我想在不使用任何其他gem的情况下使用rspec对“accepts_nested_attributes_for:skill”进行测试。我怎样才能做到这一点? 最佳答案 有方便的shouldagem匹配器用于测试accepts_nested_attributes_for,但您提到您不想使用其他gem。因此,仅使用Rspec,想法是设置包含所需Tester属性的attributes散列和包含所需skill_attributes的嵌套散列技能属性;然后传入Tes
我已经安装了“guard”和“guard-rspec”,我还配置了Guardfile(以观察“app/views”中的变化)但是当我运行“bundleexecguard”时,我总是得到这个:vagrant@vagrant-debian-squeeze:/vagrant/sample_app$bundleexecguardGuardcouldnotdetectanyofthesupportednotificationlibraries.Guardisnowwatchingat'/vagrant/sample_app'Guard::RSpecisrunning,withRSpec2!Run
我有一个将代码块作为参数的方法。问题是:如果此方法调用了block,如何使用RSpec进行测试?可以在方法需要的任何范围内评估block,不一定使用yield或block.call。它被传递给另一个类,或者在匿名类对象或其他地方对其进行评估。为了通过测试,作为方法调用的结果在某处评估block就足够了。有没有办法使用RSpec来测试这样的东西?另见this对于使用let和模拟的更复杂的情况。 最佳答案 对于这类问题,我喜欢使用throw而不是raise,因为它不能被任意的救援处理程序拯救。所以它可能看起来像这样:my_proc=pr
我在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(:
在rspec中,您可以测试对方法的调用是否接收到一个参数,该参数是一个散列并且包含某些键或键值对。即:my_object.should_receive(:my_method).with(hash_including(:a=>'alpha'))有没有什么可以用数组完成类似的匹配?看起来像什么?my_object.should_receive(:my_method).with(array_including('alpha')) 最佳答案 这个怎么样:my_obj.should_receive(:my_method)do|arg|arg.
我正在尝试通过rspec(在Rails中)通过[theoriginalobject].to_json和响应主体之间的相等性来测试JSON响应。问题是响应JSON字符串中的updated_at和created_at字段与原始对象中的相同字段相差毫秒,因此测试失败。测试:lesson=FactoryGirl.create(:lesson)request.accept="application/json"get:show,{:id=>lesson.to_param,:location_id=>lesson.location_id},valid_sessionresponse.body.sho
我有一个奇怪的问题..rspec在spec/routing中生成了一个名为menus_routing_spec.rb的类测试失败,因为菜单是餐厅的嵌套资源。这是我的测试:describeMenusControllerdobefore:eachdo@restaurant=FactoryGirl.create(:random_restaurant)@menu=FactoryGirl.create(:menu)enddescribe'routing'doit'routesto#index'doparams={}params['restaurant_id']=@restaurant#get('
我正在阅读'Betterspecs'页面,并在其中一个示例中说:context'whenloggedin'doit{is_expected.torespond_with200}endcontext'whenloggedout'doit{is_expected.torespond_with401}end我不认识这个。我通常会这样做:context'whenloggedout'doit'respondswitha401'doexpect(response).toeq(401)endend这是什么语法? 最佳答案 这是在Rspec3.XX