Struggling with basic table-based models

4 views
Skip to first unread message

cpr

unread,
Nov 12, 2009, 10:24:55 PM11/12/09
to pickle
Using factory_girl underneath pickle/cucumber.

First, just an observation/question:

Scenario: one
Given I am logged in as user cpr
# user "cpr" doesn't get stored because it doesn't have a name
# The step is calling: create_model("a user", "login: \"cpr\"")
# and is properly creating the User object
# It's actually stored in pickle under the empty string so it's
there for 'the user'
And a foo exists with user: the user

Is my comment true? That pickle depends on a model having a 'name'
field in order to directly reference it? Is there a way to reference
by a different field (like "login")?

The larger issue I'm having is that I can't get tables to work at all:

Assume that Parent has_one :child

Given a child exists with name: "child1"
And the following parents exist
| name | Child |
| parent1 | child1 |

I get an error that a Child was expected but got a string. I've tried
each of these lines as well:

| parent1 | "child1" |
| parent1 | child child1 |
| parent1 | child "child1" |

to no avail.

I must be missing something pretty basic. Any pointers or slaps upside
the head will be appreciated.

Thanks,
-Chris




nruth

unread,
Nov 14, 2009, 12:30:26 PM11/14/09
to pickle
I'll have a shot at your first question for now, and come back to the
second one.

"the user" is synonymous with "the last user", as in the last user you
talked about in your scenario. This conversational style of
referencing or 'pickled' objects is something that would benefit from
a bit of a tutorial/wiki page but nobody has got round to writing it
yet.

So when you say "And a foo exists with user: the user"
you are saying "And a foo exists with user: <whatever user model I
last talked about with a pickle step in this scenario>"

you can reference other ones by adding labels to them, like

Given a user "Freddie" exists with login: "cpr"
And a foo exists with user: user: "Freddie"

This doubling up of user: user: is not ideal, but it isn't something
that's been resolved in pickle yet.
The issue is the first user: is your model attribute, while the second
one is used by pickle to resolve which model you're talking about.

It may help to think about it this way (though this notation isn't
supported)

Given [user "Freddie"] exists with login: "cpr"
And a foo exists with user: [user "Freddie"]

This alias or label you can use to refer to models other than "the
model" is not stored in your application at all, it is something done
by pickle, so you don't need a name field on your models to use pickle
for testing.

cpr

unread,
Nov 14, 2009, 8:15:58 PM11/14/09
to pickle
Thanks for the response. I kept digging and found that all I had to do
was add the login name to the create_model as:

create_model("a user \"#{login}\"", "login: \"#{login}\"")

and that part then worked fine.

As for referring to named instances in tables and in fields, I came up
with this patch:

http://codesnippets.joyent.com/#2381

that I drop in my .../config/initializers/ directory and get what I
need. Highly likely that I've missed something or have created some
undesired side-effects. Would be great if you took a look and let me
know what features I hosed.

-cpr

nruth

unread,
Nov 18, 2009, 11:34:52 AM11/18/09
to pickle
I think what you're after out-of-the-box would be something like this:

Given a child: "child1" exists
And the following parents exist:
| parent_alias | Child_alias |
| parent1 | child1 |

with step:

Given /^the following parent child: "(.*)" exists$/ do |
parent_child_table |
parent_child_table.hashes.each do |parent_child_hash|
parent = parent_child_hash[:parent]
child = parent_child_hash[:child]
Given "a parent: \"#{parent}\" exists with child: child "#{child}"
end
end

And that should be it. You may like to use the pickle create model
function rather than dynamically creating another given step. I used
it here, because to be honest I am yet to learn the pickle internals
properly, I re-use steps whenever I can as they are defined within my
app and much easier for me to find.

You may find these bits from the pickle rdoc useful:
http://ianwhite.github.com/pickle/doc/files/README_rdoc.html

You can refer to other models in the fields
-------------------------------------------

Given a user exists
And a post exists with author: the user

Given a person: "fred" exists
And a person: "ethel" exists
And a fatherhood exists with parent: user "fred", child: user "ethel"

And if you want to test your associations (rather than create, as you
are doing here)

Asserting associations
----------------------
One-to-one assocs: “Then a model should be other model’s association“,
e.g.

Then the person: "fred" should be person: "ethel"'s father
Many-to-one assocs: “Then a model should be [in|one of] other model’s
association“, e.g.

Then the person: "ethel" should be one of person: "fred"'s children
Then the comment should be in the post's comments

Ian White

unread,
Nov 24, 2009, 5:10:58 PM11/24/09
to pickle
The above discussion assumes that pickle refs work in tables, which up
until 0.2 wasn't actually a feature (oops)

Try out 0.2 to see if it floats your boat with tables

(you can also find using tables now as well)

Cheers,
Ian
Reply all
Reply to author
Forward
0 new messages