Rspec accessing the wrong id?

Hi,

This is probably straightforward to solve. I’m new to rspec so I hope
you’ll be patient with me. I’ve created a model method which works in
practice, but which rspec is not passing. I’m obviously not going
about things in the right way in rspec so I’d appreciate any advice
offered.

Here’s the (releveant parts of the) method which sets up a one to many
relationship between Company and Projects.

def.self create_project(record)
parent_company = Company.find_by_basecampid(record.company.id)
project = self.find_or_initialize_by_basecampid(record.id)
project.company_id = parent_company.id
end

In rspec I’ve got this:

def parent_company_attributes
{
:id => 1,
:basecampid => 448718,
:name => “The Media Collective”
}
end

before do

mock_record
Company.should_receive(:find_by_basecampid).
with(parent_company_attributes[:basecampid]).
and_return(parent_company_attributes)

@project = Project.create_project(record)

end

it “should populate company_id” do
@project.company_id.should eql(parent_company_attributes[:id])
end

The problem is that it looks like rspec is using the object id rather
than the id from parent_company_attributes. Here’s the failure
message:

‘Project.create project record should populate company_id’ FAILED
expected 1, got 8817400 (using .eql?)

Thanks for any insights

David C.

On 6.2.2008, at 11.16, David C. wrote:

 :id => 1,

and_return(parent_company_attributes)
You’re returning a hash here. If you call id for hash (like you do in
this case), it will return the object id, not the element with key :id.

I think what you want to do is to create a mock_model with that hash
and then stub Company.find_by_basecampid to return that mock object.

//jarkko

David C.


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users


Jarkko L.
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs