[rspec-users] Reusing story snippets

Jim Morris wolfmanjm at gmail.com
Tue Jun 17 03:28:28 EDT 2008


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)

Then I define my steps using StepGroup.new

Then the Scenarios

It seems to work although not very intuitive :)

I'd prefer a before(:all) and after(:all)

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.
>
> 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.
>
> 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 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


More information about the rspec-users mailing list