是否可以检查传递给方法调用参数的参数是否满足某些约束条件。我想做类似的事情my_double=double("MyObject")my_double.should_receive(:mocked_method).with{}非常感谢。编辑:我会试着澄清一点我想要完成的事情我想要的是检查一个给定的模拟方法是否被调用并传递一个满足某些条件的对象 最佳答案 备案。我想完成的可以用@test_double.should_receive(:send_mail)do|mail|mail.to.shouldeq(["john@aol.com"])
我有这样的情况:moduleSomethingdefmy_methodreturn:some_symbolendendclassMyClassincludeSomethingdefmy_methodifxxx?:other_symbolelsesuperendendend现在问题出在测试上——我想确保从覆盖方法调用super方法并将其stub,以便我可以测试方法的其他部分。我如何使用RSpec模拟来实现这一点? 最佳答案 确保super被调用听起来很像测试实现,而不是行为,并且模拟被测对象无论如何都不是一个好主意。我建议只明确指定不
模型:classAttachmenttrue,:size=>{:less_than=>20.megabytes}end无效的测试:describeAttachmentdoit{shouldvalidate_presence_of:file}it{shouldvalidate_size_of:file}#validate_size_ofdoesnotexistend我想避免将20MB的文件转储到存储库中只是为了测试它。有没有一种与我上面尝试过的方法类似的方法确实有效? 最佳答案 我做到这一点的最好方法是使用内置的shouldamatc
我在我的项目中遇到过这种情况——我有一个Singleton类代表测试期间使用的浏览器:classBrowserincludeSingletondefinitialize@browser=Watir::Browser.new:ffenddefgotourl@browser.gotourlenddefclose@browser.closeendend我想通过这个rakefile确保浏览器在测试完成后关闭:desc"defaulttesttask"task:test_alldoRake::Task[:all_rspec_tests].invokeRake::Task[:close_brows
出于某种原因,我的Rspec测试是否在我期望引发异常时失败。我正在运行Rspecv2.14.1,这是一个自定义Ruby应用程序,不是Rails应用程序。示例代码:#test.rbclassTestclassBadError规范文件:#test_spec.rbrequire'spec_helper'require'test'describeTestdoit'raisesanexception'doexpect(raiseTest::BadError).toraise_exception(Test::BadError)endend结果:FFailures:1)Testraisesanexc
所以我有以下内容:foo.eachdo|f|f.begindo_stuffdo_more_stuffendend然后我通过调用and_yield()来模拟f对象。我希望能够通过将原始block{do_stuffdo_more_stuff}传递给begin方法来测试begin方法,而不是模拟实现....我不能只让begin方法在mock上被调用而不至少stub,所以怎么办我愿意? 最佳答案 同样,我发现了一个未记录的功能:allow(thing).toreceive(:foo)do|_,&block|block.callend叹息..
我有一个类:classApiParserdefinitialize..enddefapi_data#returnsanarrayofhasheslikeso:#[{answer:"yes",name:"steveb"age:22,hometown:"chicago",...},#{answer:"unsure",name:"tomz",age:44,hometown:"baltimore",...},#{answer:"no",name:"thebrah",age:nil,hometown:"SF",...},#{...},{...},...]endend该方法返回一个哈希数组。数组的
在我的Ruby应用程序中,我尝试使用RSpec测试一个使用来自Controller的参数调用的模块方法。有问题的模块是用于跟踪分析的EventTracker模块:https://github.com/doorkeeper/event_tracker情况是这样的:在我的Controller中,我调用Controller中的方法,如下所示:classInvestorHomeControllertrack_event方法定义在EventTracker模块中,如下所示:moduleEventTrackermoduleHelperMethodsdeftrack_event(event_name,
我在ruby应用程序(非Rails)中加载依赖项时遇到问题。它的文件夹树项目->bin->lib->modules->file1.rb->file2.rb->spec->file2_spec.rb我的文件是require'file1'moduleFile2end我的规范是require'spec_helper'require_relative'../lib/modules/file2'在错误信息中显示rspec规范/query_util_spec.rb/home/gustavo/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_
这是一个非常简单的问题,但我找不到任何解决方案。已经3天了。请帮忙。所以我有这个用户Controller.rbdefindex@users=@users.normal.order(:name).pageparams[:page]end所以我在我的users_controller_spec.rb中有这个it"assignallnormalusersto@users"dousers=User.normal.allget:indexexpect(assigns(:users)).toeq(users)end我的代码基于https://www.relishapp.com/rspec/rspec