[rspec-users] Dealing with dependent data

Mikel Lindsaar raasdnil at gmail.com
Wed Jun 25 21:38:51 EDT 2008

On Thu, Jun 26, 2008 at 4:37 AM, Christopher Bailey
<chris at cobaltedge.com> wrote:
> If there's already been a thread on this, let me know (and if you can,
> point me to it)...

I asked something similar about a week or so ago, you can see it here:


It is on 'reusing story specs'

But I have done some more work since then...

> In building my stories, I often need to setup a bunch of data where
> you have multiple levels of models that depend on each other.  For

> Scenario: view doctors
>  Given: existing locations
>  And: existing organizations
>  And: existing doctors

You can use plain text stories and use multiple steps_for to get a
similar effect.

But in this case, on that set of stories, it would make sense to
reduce that Given block down to:

# viewing doctors story file...
Scenario: view doctors
  Given: existing doctors in organizations that have a location

And then I would use some helpers or fixture loading to handle your
data loading...

# spec helper file...
def build_valid_doctor(params)
  location = Location.create(:blah...)
  org = Organization.create(:location => location)
  Doctor.create({:organization => org}.merge(params))

# steps file....
steps_for :viewing_doctors do

  Given "existing doctors in organizations that have a location"
    build_valid_doctor(:surname => 'Hyde')
    build_valid_doctor(:surname => 'Jeckle')


Of course you can make those factory methods do all sorts of things...

Hope that helps


Rails, RSpec, Puppet andLife blog....

More information about the rspec-users mailing list