[rspec-users] Dealing with dependent data

Christopher Bailey chris at cobaltedge.com
Wed Jun 25 22:18:54 EDT 2008

In reply to both of the last two replies to my post...

I am already doing the factory stuff for data creation.  My question
is more about dependencies between fixture data sets.

So, you have 3 related models: A, B, C.  Model A can stand on its own.
 Model B can't exist without an A, and C can't exist without a B
(which means it also indirectly requires an A).  I have factories that
create these things, and helper methods that create sets of them
(since I need fairly specific relationships, not just randomly
generated ones like say object_daddy or factory_girl plugins/gems help
with (although I have used object_daddy and like it - tried to use
factory girl, but had problems).

It's not really a true "problem", it's more that there is an inherant
dependency here, where there isn't a very expressive way of stating
that.  So, more a point of discussing how other folks are handling
this and making things clear in their code that their are
dependencies.  The idea to combine the 3 givens is a point I'll look
at, as I think I agree that that probably conveys the dependency and
association between the three models better.

The other thing I'm finding a lot is that I have a lot of the same
needs for this fixture type data between my regular RSpec examples
(model tests mostly, as I'm going light on controller and view tests
and mostly doing stories) and my RSpec stories.  Are folks using say a
single helper/factory to generate fixture data for both?  I guess I
only just though of sharing it between them now, as there really is no
technological barrier to doing so, although in my examples I do tend
to leverage mocks and stubs when I can, but stay strictly "real" with

On Wed, Jun 25, 2008 at 7:07 PM, Scott Taylor
<mailing_lists at railsnewbie.com> wrote:
> On Jun 25, 2008, at 9:38 PM, Mikel Lindsaar wrote:
>> 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:
>> http://www.ruby-forum.com/topic/156392
>> 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
>>  When...
>> 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))
>> end
>> # 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')
>>  end
>> end
>> Of course you can make those factory methods do all sorts of things...
> It really seems you are asking two questions: One about reusability, and one
> about setting up data.
> I can't add anything to the first, but factories are definitely the way to
> go regarding the second issue (setting up the data).  Check out this post by
> Dan Manges (and look at the fixture options at the end of the post):
> http://www.dcmanges.com/blog/38
> Scott
>> Hope that helps
>> Mikel
>> --
>> http://lindsaar.net/
>> Rails, RSpec, Puppet andLife blog....
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Christopher Bailey
Cobalt Edge LLC

More information about the rspec-users mailing list