草庐IT

ruby - 在使用 any_instance 的情况下,如何让 stub 返回调用它的实例?

例如,我有A类。classAend并希望在规范中从stub方法返回该类的实例。A.any_instance.stub(:my_method).and_return()是否有可能在RSpec中做出类似的东西? 最佳答案 这将为您解决问题:A.any_instance.stub(:my_method)do|*args|instance=RSpec::Mocks::space.send(:receivers).lastend我从这里的rspec代码中挖出了这个:rspecgithubcode附言:这完全取决于rspec的实现,将来可能会改

ruby-on-rails - 如何 stub after_create 回调保存!在模型中?

我收到以下错误:输出:1)LabelsController#createlabelisnewcreatesanewlabelFailure/Error:post:create,attributes[:label],format::jsonNoMethodError:undefinedmethod`save!'fornil:NilClass#./app/models/labeling.rb:17:in`update_target'在标签模型中:after_create:update_targetdefupdate_targetself.target.save!end测试:require'

ruby - 有哪些方法可以从 WSDL 文件自动生成 ruby​​ 客户端 stub ?

我正在使用Ruby和Savongem与SOAP/WS交互,并希望从Ruby中的WSDL自动生成客户端请求方法。在我这样做之前,我想知道是否有任何其他Ruby/SOAP库可以执行此操作?编辑:请注意,我已经知道这在Savon中不可用,事实上我的目的是添加该功能,我正在检查它是否存在于其他地方ruby。 最佳答案 因为你问这个问题才几天,而我遇到了同样的问题,我决定创建一个小脚本来做到这一点。下载-例如保存为objects.rb并使用_bundeexecobjects.rbpath_to.wsdl_运行https://gist.gith

ruby-on-rails - RSpec stub 邮件程序

我有一个UserMailDispatcher类,它的工作是根据特定条件通过ActiveMailer发送邮件。我正在尝试使用RSpec对其进行测试,但效果不佳。我想以某种方式stub测试邮件并查看该类是否正确传递它。这是我到目前为止所拥有的:我所有的邮件程序都继承自ApplicationMailer:application_mailer.rbclassApplicationMaileruser_mail_dispatcher_spec.rbrequire'rails_helper'describeUserMailDispatcherdoclassUserMailer我想测试调度程序是否可

ruby-on-rails - 具有属性的 rspec stub any_instance

我需要对具有特定属性或属性集的模型的所有实例进行stub。例如,使用ActiveRecord:let(:model1){Model.create!(uid:1)}let(:model2){Model.create!(uid:2)}beforedoallow(model1).toreceive(:foo).and_return:barallow(model2).toreceive(:foo).and_return:bazenditdoexpect(model1.foo).toeq:bar#=>passesexpect(model2.foo).toeq:baz#=>passes######

ruby-on-rails - Rspec stub 不起作用

我有以下规范来测试Controller方法:context"#create"doit"shouldredirectwhenmodelisvalid"doUser.stub!(:valid?).and_return(true)post:create,:user=>FactoryGirl.attributes_for(:user)response.shouldredirect_to("/")endit"shouldrendernewtemplatewhenmodelisinvalid"doUser.stub!(:valid?).and_return(false)post:create,:u

ruby - 对如何使用模拟和 stub 感到困惑

我有一个类和一个规范。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

ruby-on-rails - 使用 FactoryGirl 构建一个 json stub

我尝试使用工厂构建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下面是我正在尝试构建的

ruby - 如何使用 RSpec 模拟/ stub 文件目录及其内容?

前一段时间我问了“HowtotestobtainingalistoffileswithinadirectoryusingRSpec?”,虽然我得到了一些有用的答案,但我仍然卡住了,因此提出了一个新问题,其中包含有关我正在尝试做的事情的更多细节。我正在编写我的第一个RubyGem。它有一个模块,其中包含一个类方法,该方法返回一个数组,该数组包含指定目录中的非隐藏文件列表。像这样:files=Foo.bar:directory=>'./public'该数组还包含一个表示文件元数据的元素。这实际上是从文件内容生成的哈希值的哈希值,其想法是即使更改单个文件也会更改哈希值。我已经编写了待定的RS

ruby - RSpec stub : return in a sequence

我知道以下事情有效:返回一个参数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