如何存根活动记录关系以使用rspec测试where子句?

时间:2021-05-17 20:17:25

I've got a class that look like this:

我有一个看起来像这样的课:

class Foo < ActiveRecrod::Base
  has_many :bars

  def nasty_bars_present?
    bars.where(bar_type: "Nasty").any?
  end

  validate :validate_nasty_bars
  def validate_nasty_bars
    if nasty_bars_present?
      errors.add(:base, :nasty_bars_present)
    end
  end
end

In testing the #nasty_bars_present? I'd method like to write an rspec test that stubs the bars association but allows the where to execute naturally. Something like:

在测试#nasty_bars_present?我想方法就是编写一个rspec测试来存根吧关联但允许自然执行的地方。就像是:

describe "#nasty_bars_present?" do
  context "with nasty bars" do
    before { foo.stub(:bars).and_return([mock(Bar, bar_type: "Nasty")]) }
    it "should return true" do
      expect(foo.nasty_bars_present?).to be_true
    end
  end
end

The test above gives an error about there being no method where for an array. How can I wrap the mock so the where will execute appropriately?

上面的测试给出了关于没有数组方法的错误。如何包装模拟以便在哪里执行适当的?

Thanks!

1 个解决方案

#1


4  

For RSpec 2.14.1 (it should also work for RSpec 3.1), I would try this:

对于RSpec 2.14.1(它也适用于RSpec 3.1),我会尝试这样做:

describe "#nasty_bars_present?" do
  context "with nasty bars" do
    before :each do
      foo = Foo.new
      bar = double("Bar")
      allow(bar).to receive(:where).with({bar_type: "Nasty"}).and_return([double("Bar", bar_type: "Nasty")])
      allow(foo).to receive(:bars).and_return(bar)
    end
    it "should return true" do
      expect(foo.nasty_bars_present?).to be_true
    end
  end
end

This way, if you call bars.where(bar_type: "Nasty") without the specific conditions in the where statement, you won't get the bar double with bar_type: "Nasty". It could be reusable for future mocking of bars (at least for returning a single instance, for multiple instances, you would add another double).

这样,如果在没有where语句中的特定条件的情况下调用bars.where(bar_type:“Nasty”),则不会使用bar_type:“Nasty”来获取条形图。它可以重复用于将来模拟条形图(至少对于返回单个实例,对于多个实例,您将添加另一个实例)。

#1


4  

For RSpec 2.14.1 (it should also work for RSpec 3.1), I would try this:

对于RSpec 2.14.1(它也适用于RSpec 3.1),我会尝试这样做:

describe "#nasty_bars_present?" do
  context "with nasty bars" do
    before :each do
      foo = Foo.new
      bar = double("Bar")
      allow(bar).to receive(:where).with({bar_type: "Nasty"}).and_return([double("Bar", bar_type: "Nasty")])
      allow(foo).to receive(:bars).and_return(bar)
    end
    it "should return true" do
      expect(foo.nasty_bars_present?).to be_true
    end
  end
end

This way, if you call bars.where(bar_type: "Nasty") without the specific conditions in the where statement, you won't get the bar double with bar_type: "Nasty". It could be reusable for future mocking of bars (at least for returning a single instance, for multiple instances, you would add another double).

这样,如果在没有where语句中的特定条件的情况下调用bars.where(bar_type:“Nasty”),则不会使用bar_type:“Nasty”来获取条形图。它可以重复用于将来模拟条形图(至少对于返回单个实例,对于多个实例,您将添加另一个实例)。