Matt Connolly's Blog

my brain dumps here…

Testing with cucumber – session state

I was recently at the Brisbane Ruby on Rails meetup where one discussion was about the speed of testing. I’ve also read in many places that rails apps are terribly slow at testing.

In a recent rails app I’m working on, I wrote several features describing how I want the user sign up sign in and sign out to function. However following this, my next features about a user’s data also required me to repeat the user sign in step in every scenario. While it’s not too hard for each scenario to start with “given I am signed in (as a role)”, or put that step in. “Background:” section, it does mean that the capybara steps are repeated for every single scenario.

So, I started to explore how a scenario can skip the sign in web steps, and it appears it cannot be done (easily). This stack overflow discussion is very relevant: http://stackoverflow.com/questions/1271788/session-variables-with-cucumber-stories

I do find it very odd that cucumber steps can access and modify the rails database (eg load fixtures / create objects with factories and save them) but cannot access the session state. The same applies to rspec request specs.

I guess that’s just the way it is, but in the interest of speeding things up, I’d like to know if there’s any special reasons for this limitation that prevents us from speeding up these tests.

Advertisements

2 responses to “Testing with cucumber – session state

  1. marnen 6 June, 2012 at 02:26

    Cucumber scenarios should do as much as possible through the UI, and should not really be mocking or stubbing. What I’d recommend for your case, instead of disabling login, is to write *one* step that you can put in the background for any scenario where you need it — that is, just make “Given I am logged in” run the entire multistep process. If that’s still too much, then use a @tag.

    • mattconnolly 6 June, 2012 at 14:55

      Agreed. I’ve learnt a bit about using cucumber since I first wrote this, and in line with them removing the web steps, it’s far better to simplify the cucumber steps and put as much of the specific actions into the step definitions.

      I actually really like the Spinach approach to doing it, but haven’t found the tools (like RubyMine) integrate with it as well.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: