BDD / cucumber thought - chaining 'Then' steps with Which

Going back to the debate about keeping state between steps, I found
myself with the mild urge to be able to write this today:

Given there is a user
And the user has 20 friends
Then I should see a thumbnail of each of the users’s friends
Which should be a link to the user profile page for that friend

NB: An idiom in this which may be songkick-only but was inspired by
what we’ve read on this list, and worth explaining: “the user” as in
“the user has 20 friends”, means User.first in rails. If there’s more
than one user when this step runs, it will flunk, because the English
no longer works.

So anyway, what I was thinking was that it would be nice to be able
to refer to the last thing I said I should be able to see, and
decorate it with more specification in a subsequent step or steps.

Obviously, I could roll these into one long Then step, but maybe this
is more elegant…

Just thinking out loud. I have no idea what the steps code would look
like.

WDYT?

cheers,
Matt

http://blog.mattwynne.net

In case you wondered: The opinions expressed in this email are my own
and do not necessarily reflect the views of any former, current or
future employers of mine.

Stories without shared state feel clunky to me, because we would never
speak
that way.

I also had a similar urge, don’t know if it’s a “programmer” urge:
Given a user
With the name ‘josh’
And the password ‘sesame’

…so I don’t end up with an explosion of steps for ‘a user with x’, ‘a
user
with y’, ‘a user with x and y’ .

So there are named things, but we also often want to refer to the
anonymous
“last mentioned thing” like we do in speech. So I end up doing things
like
this sometimes:

Given /a user/ do
shared_state[“he”] = shared_state[“she”] = shared_state[“it”] =
User.new
end

…is that bad?

Cheers,

Josh

On Fri, Sep 26, 2008 at 6:30 AM, Matt W. [email protected] wrote:

So anyway, what I was thinking was that it would be nice to be able to refer
to the last thing I said I should be able to see, and decorate it with more
specification in a subsequent step or steps.
Obviously, I could roll these into one long Then step, but maybe this is
more elegant…
Just thinking out loud. I have no idea what the steps code would look like.
WDYT?

One of the promises of using treetop is the ability to extend the
grammar yourself to include keywords like Which.

As for maintaining state across steps, I know that Aslak recommends
avoiding this on the cucumber wiki, but I can tell you I’ve not once
(as in never ever ever) been bitten by doing this. I’ve got a couple
of helper methods I use to create and retrieve instance variables that
are uniquely named. See 13087’s gists · GitHub.

Including that module in env.rb allows me to do this:

Given a user with login “ernie” and password “rubber”
And a user with login “bert” and password “duckie”
And ernie is assigned the admin role
And bert is assigned the contributor role

Given /^a user with login “(.)" and password "(.)”$/ do |login,
password|
create_user_named(login, password) do |user|
set_ivar :user, login, user
end
end

Given /^(.) is assigned the (.) role$/ do |login, role|
get_ivar :user, login do |user|
user.add_role(role)
end
end

… thus creating and retrieving instance variables named user_ernie
and user_bert with different roles.

Other people have experiences, positive or negative, sharing state
across steps with instance variables?

On 26 Sep 2008, at 13:59, Josh C. wrote:

x’, ‘a user with y’, ‘a user with x and y’ .
…is that bad?
FWIW, I think it’s rather nice. We went through a fad of using @it
for a while, and now we have a stuff[] hash. Both similar ideas -
there must be something in this.

Aslak, I’d love to hear a war story about how shared state bit you in
the arse to give this debate some balance.

cheers,
Matt

http://blog.mattwynne.net

In case you wondered: The opinions expressed in this email are my own
and do not necessarily reflect the views of any former, current or
future employers of mine.

On 26 Sep 2008, at 13:33, David C. wrote:

by what
specification in a subsequent step or steps.
Obviously, I could roll these into one long Then step, but maybe
this is
more elegant…
Just thinking out loud. I have no idea what the steps code would
look like.
WDYT?

One of the promises of using treetop is the ability to extend the
grammar yourself to include keywords like Which.

Wow. OK I need to check that out.

are uniquely named. See 13087’s gists · GitHub.

Other people have experiences, positive or negative, sharing state
across steps with instance variables?

So far, my most remarkable experience is that we had some pretty in-
depth debates in the office about whether it was a good or bad thing!
It’s certainly a hot topic.

cheers,
Matt

http://blog.mattwynne.net

In case you wondered: The opinions expressed in this email are my own
and do not necessarily reflect the views of any former, current or
future employers of mine.

On Fri, Sep 26, 2008 at 7:59 AM, Matt W. [email protected] wrote:

One of the promises of using treetop is the ability to extend the
grammar yourself to include keywords like Which.

Wow. OK I need to check that out.

I don’t know how directly this is supported at this point - when I
said “promises” I meant that in the future sense :slight_smile:

David

On Sep 26, 2008, at 9:06 AM, Matt W. wrote:

FWIW, I think it’s rather nice. We went through a fad of using @it
for a while, and now we have a stuff[] hash. Both similar ideas -
there must be something in this.

I’m doing the same thing. I had @current_project, @current_page etc
( http://www.mail-archive.com/[email protected]/
msg06272.html ) but now keep it in a hash @current[:project] etc.

I encountered a problem with things like “When I click the first item
in the list” or even “When I visit /foos/123”, my story doesnt know
what state its in after that. But then I realized the state is
embedded in the url !!

This is a RESTful rails app so I wrote a little helper that parses
the current url to reset my instance variables, which looks something
like this (mine actually does more, like strips off trailing anchors
(#foo), assigns variables into @current (?foo=123), and captures
subdomain.test.com in an :account)

def current_resources( url = nil )
url ||= response.request.request_uri

split resources

bits = url.split(‘/’)
bits.shift # initial ‘/’

assign resources

while !bits.blank?
@current[ bits.shift.to_sym ] = bits.shift
end
@current
end

So after any get, post, visits, clicks etc I call current_resource
helper