Ruby on rails Rspec can';t stub where或find_by_,仅查找
我将分享几对运行代码和测试代码。基本上,测试代码只有在我对对象类使用Ruby on rails Rspec can';t stub where或find_by_,仅查找,ruby-on-rails,ruby-on-rails-4,rspec,rspec-rails,stub,Ruby On Rails,Ruby On Rails 4,Rspec,Rspec Rails,Stub,我将分享几对运行代码和测试代码。基本上,测试代码只有在我对对象类使用find时才起作用,但问题是find是我不想使用的方法,因为我没有寻找主键 方法1:使用计划对进行存根:其中。所有,以便可以在其上调用第一个 #run code @current_plan = Plan.where(stripe_subscription_id: event.data.object.lines.data.first.id).first #test code @plan = Plan.new #so this i
find
时才起作用,但问题是find
是我不想使用的方法,因为我没有寻找主键
方法1:使用计划对进行存根:其中。所有,以便可以在其上调用第一个
#run code
@current_plan = Plan.where(stripe_subscription_id: event.data.object.lines.data.first.id).first
#test code
@plan = Plan.new #so this is the first Plan that I'd like to find
Plan.stub(:where).and_return(Plan.all)
#result of @current_plan (expecting @plan)
=> nil
方法2:链式短桩:其中和:第一个
#run code
@current_plan = Plan.where(stripe_subscription_id: event.data.object.lines.data.first.id).first
#test code
@plan = Plan.new #so this is the first Plan that I'd like to find
Plan.stub_chain(:where, :first).and_return(@plan)
#result of @current_plan (expecting @plan)
=> nil
方法3:存根自定义:查找方法
#run code
@current_plan = Plan.find_by_stripe_subscription_id(event.data.object.lines.data.first.id)
#test code
@plan = Plan.new
Plan.stub(:find_by_stripe_subscription_id).and_return(@plan)
#result of @current_plan (expecting @plan)
=> nil
方法4:stubing:find
有效!但我无法通过主键找到。。。所以我理想情况下需要方法3来工作
#run code
@current_plan = Plan.find(2) #for giggles, to make sure the stub is ignoring the 2 argument
#test code
@plan = Plan.new
Plan.stub(:find).and_return(@plan)
#result of @current_plan (expecting @plan)
=> @plan
我想另一个答案是我如何创造性地使用:查找带有参数的,尽管我知道这不是最佳实践…好的,我有一个临时解决方案,就是使用select。即
#run code
@current_plan = Plan.select { |p| p.stripe_subscription_id == event.data.object.lines.data.first.id }.first
#test code
@plan = Plan.new
Plan.stub_chain(:select, :first).and_return(@plan)
#result of @current_plan (expecting @plan)
=> @plan
尽管如此。。。如果其他人有想法,请插话。。。我现在在学术上有点好奇,为什么where
或where,first
存根不起作用
find\u by\u stripe\u subscription\u id
,我已经做了更多的测试,甚至在定制的find\u by
方法上,方法的期望也失败了,所以存根当然不起作用。但是,请看下面齐蒂人的答案,也许上帝只恨我…你可以删除那些方法。所有这些测试均通过:
require 'rails_helper'
RSpec.describe Foo, type: :model do
let(:foo) { double(name: "foo") }
it "works with find" do
expect(Foo).to receive(:find).and_return(foo)
expect(Foo.find(1)).to eq foo
end
it "works with find_by" do
expect(Foo).to receive(:find_by_name).and_return(foo)
expect(Foo.find_by_name("foo")).to eq foo
end
it "works with where" do
expect(Foo).to receive(:where).and_return([ foo ])
expect(Foo.where(name: "foo")).to eq [foo]
end
end
如果看不到至少一个完整的例子来说明问题,就很难看到这里发生了什么。是否设置@current_plan
值的代码从未执行过?如果您认为这会有所帮助,我很乐意添加更多代码,但我使用方法4作为基准来说明,这种方法工作得很好,为什么其他方法不行。从字面上说,这两种方法的区别在于我只是在转换代码。让我知道如果你想看到更多的代码,你可以尝试添加一个期望,以看到你正在存根的方法实际上被调用。我看不出有任何理由可以存根:find但不能:find_by。是的,我试过了。所有标准方法(查找、位置等)均已收到<代码>按条带查找订阅id
不是。。。我认为Rspec matcher不能与customfind\u by
一起使用。无论如何,即使对于where
方法,它被成功检索的事实也无助于它没有被删除的事实。。。好啊所以我不知道该说什么。有些地方可能出了问题,但我真的不知道在哪里,什么地方。不过还是要谢谢你。