[rspec-users] Cucumber - stub! or mock?
Scott Taylor
mailing_lists at railsnewbie.com
Tue Sep 16 21:05:43 EDT 2008
On Sep 15, 2008, at 4:14 PM, Tim Glen wrote:
> Hey all,
>
> I've got some code that I (mostly) inherited. It essentially has a
> couple of AR class methods that look for a specific record by id:
>
> class Project < ActiveRecord::Base
> class << self
> def specific_project
> @another_specific_project ||= Project.find(10) if
> Project.exists?(10)
> end
>
> def another_specific_project
> @specific_project ||= Project.find(11) if Project.exists?(11)
> end
> end
> end
Why don't use just use a slug for these things? In other words - use a
unique name for each record, since the numbers 10 and 11 don't mean
much to anyone.
Then, use a factory (like FixtureReplacement or Object Daddy) to
generate the records in env.rb. Here's how you'd do that with
FixtureReplacement (at the bottom of your env.rb file):
include FixtureReplacement
create_project(:slug => "my-unique-name-for-project1")
create_project(:slug => "my-unique-name-for-the-second-project")
Scott
More information about the rspec-users
mailing list