views:

366

answers:

1

I would like to stub the #class method of a mock object:

describe Letter do
  before(:each) do
    @john = mock("John")
    @john.stub!(:id).and_return(5)
    @john.stub!(:class).and_return(Person)  # is this ok?
    @john.stub!(:name).and_return("John F.")
    Person.stub!(:find).and_return(@john)
  end
  it.should "have a valid #to field" do
    letter = Letter.create!(:to=>@john, :content => "Hello John")
    letter.to_type.should == @john.class.name
    letter.to_id.should == @john.id
  end
  [...]
end

On line 5 of this program, I stub the #class method, in order to allow things like @john.class.name. Is this the right way to go? Will there be any bad side effect?

Edit:

The Letter class looks like this:

class Letter < ActiveRecord::Base
    belongs_to :to, :polymorphic => true
    [...]
end

I wonder whether ActiveRecord gets the :to field's class name with to.class.name or by some other means. Maybe this is what the class_name method is ActiveRecord::Base is for?

A: 

I think you should be using mock_model for this particular case. Your before(:each) would look like that:

before(:each) do
  @john = mock_model(Person, :name => "John F.")
  Person.stub!(:find).and_return(@john)
end

Then for your other question, you should not really care about how Rails works to test your behaviour. I don't think it's a good idea to test to_type and to_id fields yourself. This is Rails behaviour and as such should be tested in Rails, not in your project.

I have been using Remarkable for a while and it makes this really easy to specify:

describe Letter
  should_belong_to :to, :polymorphic => true
end
BiHi