Is it possible to call should_receive
on a non-mock object?
If it is, I'm wondering why this test doesn't pass. SiteStyle
has_many images
and when I call duplicate!
on a site_style, I want the images
to call duplicate
on themselves as well. The test fails. The actual images get duplicated though. Why is that?
SiteStyle model:
class SiteStyle < ActiveRecord::Base
belongs_to :site
belongs_to :user
validates_presence_of :name, :user
has_many :images, :as => :imageable, :order => "position", :dependent => :destroy
...
开发者_如何学C def duplicate!(user, *args)
options = args.extract_options!
to_site ||= Site.find(options[:site_id])
dup_style = self.class.create :site_id => to_site.id, :name => name, :user_id => user.id
self.images.each { |image| image.duplicate!(dup_style) }
dup_style
end
...
end
SiteStyleSpec:
require 'spec_helper'
describe SiteStyle do
...
describe "duplicate!(user, *args)" do
before do
@creator = User.make
@site = Site.make
@style = SiteStyle.make :site => @site, :user => @creator
end
...
it "should have a duplicate of the images" do
@image = Image.make :imageable_type => @style.class.name, :imageable_id => @style.id
@image.should_receive(:duplicate!).once
@dup_style = @style.duplicate!(@creator)
end
end
end
Error:
Failures:
...
2) SiteStyle duplicate!(user, *args) should have a duplicate of the images
Failure/Error: @image.should_receive(:duplicate!).once
(#<Image:0xc7b40d8>).duplicate!(any args)
expected: 1 time
received: 0 times
# ./spec/models/site_style_spec.rb:83
Your line here:
@image.should_receive(:duplicate!).once
Is working with only that specific @image instance, and calling should_receive only on it so that expectation doesn't transfer over to the image objects used inside the duplicate method (or any other place where an image is instantiated) even if it is the same record from the database.
精彩评论