[rspec-users] Rspec accessing the wrong id?
jarkko at jlaine.net
Thu Feb 7 08:57:05 EST 2008
On 6.2.2008, at 11.16, David Currin wrote:
> 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(record.company.id)
> project = self.find_or_initialize_by_basecampid(record.id)
> project.company_id = parent_company.id
> In rspec I've got this:
> def parent_company_attributes
> :id => 1,
> :basecampid => 448718,
> :name => "The Media Collective"
> before do
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.
> @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 Currin
> rspec-users mailing list
> rspec-users at rubyforge.org
More information about the rspec-users