Rspec accessing the wrong id?


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

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(
project = self.find_or_initialize_by_basecampid(
project.company_id =

In rspec I’ve got this:

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

before do


@project = Project.create_project(record)


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

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

‘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,

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.


David C.

rspec-users mailing list
[email protected]

Jarkko L.

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