[rspec-users] Reusing story snippets

David Chelimsky dchelimsky at gmail.com
Tue Jun 17 07:34:14 EDT 2008


Reordered your posts so my comments make sense (we prefer to avoid
top-posting - even though I sometimes violate that myself :) ).

> On Jun 16, 11:58 pm, Jim Morris <wolfma... at gmail.com> wrote:
>> I'm not using Rails, I am doing end to end integration testing talking
>> to the server via net/http, so RailsStory is not involved.

Cool. Are you talking directly through ruby constructs or through a
browser tool like selenium?

>> I think the listeners may do it, I can use story_started like
>> before(:all) and story_ended like after(:all) which will be great,
>> presuming story_ended is always called even after a failure.

Yep.

>> However I am missing the place where that listener gets registered, I
>> am using the...
>>
>> Story "description", %{...}, :steps_for => steps do
>>   Scenario "sdsdsd" do
>>   ...
>>   end
>> end
>>
>> syntax, so where is the listener set?
>>
>> Thanks

On Tue, Jun 17, 2008 at 2:28 AM, Jim Morris <wolfmanjm at gmail.com> wrote:
> Ahh ok think I found it...
>
> In my test file at the start...
>
> class MyListener
>  def method_missing sym, *args, &block
>    # ignore all messages you don't care about
>  end
>
>  def story_started(title, narrative)
>    puts "...Started story #{title}"
>  end
>
>  def story_ended(title, narrative)
>    puts "...Ended story #{title}"
>  end
> end
>
> Spec::Story::Runner.register_listener(MyListener.new)

Yep. That's the right way.

> Then I define my steps using StepGroup.new

You can just use the steps_for method and it'll still work.

> Then the Scenarios
>
> It seems to work although not very intuitive :)
>
> I'd prefer a before(:all) and after(:all)

I'm not with you on that, but I'm open :) We could solve the OP's
problem with something like this:

Before Each Scenario:
  Log in as admin

Scenario: editing private data
  ...

Before Each Scenario would match a Given step definition "Log in as
$role" or something like that.

I'm just not sure this really heads us down the right path. Next thing
you know we'll have Before Story, After Each Scenario, After Story,
etc and the stories are going to start looking less and less like
stories and more and more and more like code, at which point you
should be using example groups instead :)

Anybody else have opinions about that?

Cheers,
David

>>
>> On Jun 16, 6:32 pm, David Chelimsky <dchelim... at gmail.com> wrote:
>>
>> > On Jun 16, 2008, at 6:18 PM, Jim Morris wrote:
>>
>> > > Along similar lines is there a way to do the equivalent of
>> > > before(:all) and after(:all) or after(:each) in stories?
>>
>> > > Basically I have a similar situation as above, but I need to make sure
>> > > the user is logged out after each scenario. or that the user is logged
>> > > in once at the start of all scenarios then logged out after them
>> > > regardless of errors etc.
>>
>> > Each scenario is run in its own instance of RailsStory, so it's got a
>> > new session (IntegrationSession). So you *should* get this isolation
>> > implicitly.
>>
>> > There are callbacks you can use if you create and register a listener,
>> > but they won't have access to the same scope that exists inside the
>> > steps. Here's how you do it:
>>
>> > class MyListener
>> >    def method_missing sym, *args, &block
>> >      # ignore all messages you don't care about
>> >    end
>>
>> >    def run_started(num_scenarios); end
>> >    def story_started(title, narrative); end
>>
>> >    etc
>> > end
>>
>> > Seehttp://rspec.info/rdoc/classes/Spec/Runner/Formatter/Story/PlainTextF...
>> > , which implements all the callback methods (we need better docs for
>> > it, but everything is there - don't use collected_steps though - that
>> > should really be marked nodoc).
>>
>> > Cheers,
>> > David
>>
>> > > Thanks
>>
>> > > On Jun 14, 7:40 am, David Chelimsky <dchelim... at gmail.com> wrote:
>> > >> On Jun 14, 2008, at 5:03 AM, Mikel Lindsaar wrote:
>>
>> > >>> I find myself doing this:
>>
>> > >>> Scenario "logged in user visiting the home page" do
>> > >>> Given "A logged in user" do
>> > >>>   a_logged_in_user
>> > >>> end
>>
>> > >>> When "..."
>> > >>> Then "..."
>> > >>> end
>>
>> > >> Things have evolved a bit since Story Runner first came out. The
>> > >> approach you are using here is what we call in-line steps, and was
>> > >> the
>> > >> only option back then.
>>
>> > >> You can now use disconnected steps in both plain text and Ruby:
>>
>> > >> require 'steps/visitors'
>>
>> > >> Story "logged in users see more stuff", %(
>> > >>   As a registered user
>> > >>   I want to see more stuff than non-registered users
>> > >>   So I can feel like I'm getting some benefit in return for giving
>> > >>     up my personal information
>> > >> ), :steps => :visitors do
>> > >>   Scenario "logged in user visits home page" do
>> > >>     Given "I am logged in as David"
>> > >>     When "I visit the home page"
>> > >>     Then "I should see the message 'Welcome David'"
>> > >>   end
>> > >> end
>>
>> > >> # in steps/visitors
>>
>> > >> steps_for :visitors do
>> > >>   Given "I am logged in as $name" do |name|
>> > >>     # create a user w/ name and log in as that user
>> > >>   end
>>
>> > >>   When ".."
>> > >>   Then ".."
>> > >> end
>>
>> > >> This approach really cleans up the story code leaving the informative
>> > >> bits while hiding the redundant detail.
>>
>> > >> HTH,
>> > >> David
>>
>> > >>> The a_logged_in_user method is a helper method in helper.rb which
>> > >>> sets
>> > >>> up the state so that the user can browse the website.
>>
>> > >>> Later in the story of course, I can just do 'Given "A logged in
>> > >>> user"
>> > >>> and it will get the previous definition.
>>
>> > >>> Is there any way to avoid that duplicated Given call at the top of
>> > >>> almost every story?
>>
>> > >>> Mikel
>> > >>> _______________________________________________
>> > >>> rspec-users mailing list
>> > >>> rspec-us... at rubyforge.org
>> > >>>http://rubyforge.org/mailman/listinfo/rspec-users
>>
>> > >> _______________________________________________
>> > >> rspec-users mailing list
>> > >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
>> > > _______________________________________________
>> > > rspec-users mailing list
>> > > rspec-us... at rubyforge.org
>> > >http://rubyforge.org/mailman/listinfo/rspec-users
>>
>> > _______________________________________________
>> > rspec-users mailing list
>> > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
>>
>> _______________________________________________
>> rspec-users mailing list
>> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list