[rspec-users] Stubbing Scopes

Christoph Schiessl chs at proactive.cc
Tue Feb 15 11:35:58 EST 2011


Hi!

I'm trying to test the following (simplified) model:

class Allocation < ActiveRecord::Base
  scope :in_interval, (proc do |start_of_interval, end_of_interval|
    params = {:s => start_of_interval, :e => end_of_interval}
    where("(starts_at > :s AND starts_at < :e) OR (ends_at > :s AND ends_at < :e) OR (starts_at <= :s AND ends_at >= :e)", params)
  end)

  scope :on_day, (proc do |day|
    day = day.to_time.beginning_of_day
    in_interval(day, day + 1.day)
  end)

  # validations and other scopes...
end

I wrote a lot of specs for :in_interval - however testing :on_day is kind of problem. I don't want to duplicate any :in_interval specs - therefore I'm trying to stub :in_interval like this:

let(:start_of_day) { Time.zone.now.beginning_of_day }
it do
  Allocation.should_receive(:in_interval).with(start_of_day, start_of_day + 1.day).and_return("result")

  # working assertion - the :in_interval stub seems to get called as expected:
  # Allocation.on_day(start_of_day + 3.hours)

  # failing assertion:
  Allocation.on_day(start_of_day + 3.hours).should == "result"
end

RSpec Output:

  Failure/Error: Allocation.on_day(start_of_day + 3.hours).should == "result"
  NoMethodError:
    undefined method `includes_values' for "result":String

I'm using Rails 3.0.4 and RSpec 2.5 (latest versions).

Best regards,
Christoph Schiessl


More information about the rspec-users mailing list