[rspec-users] Reusing story snippets

David Chelimsky dchelimsky at gmail.com
Mon Jun 16 21:32:04 EDT 2008


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

See http://rspec.info/rdoc/classes/Spec/Runner/Formatter/Story/PlainTextFormatter.html 
, 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-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list