www.francisfish.com

Click Bookshop to see my novels. Buddhist, father of 2, Ruby on Rails programmer for money, and fiction writer.








Latest Articles:

Archive

Tag Cloud:

Stats:

  • Entries = 358
  • Notes = 35

General Links

    follow me on Twitter

    Debugging cucumber scripts, cucumber and devise authentication

    Published: 8:03 PM GMT, Thursday, 4 March 2010

    Non Rails/Ruby people may as well ignore this one!

    Sometimes you need to see what the web server is sending back, when I'm working on rspec scripts I can print things out (which messes up the pretty output) but then see where my assumptions are wrong. I needed to be able to see what was in the response so that I could work out what was wrong. It's annoyingly easy when you work out how. You need to add a step that looks like this:

    When /^I dumped the response$/ do
         puts response.body
    end

    Then, in the features that are giving trouble:

    Scenario: creator restores fragment
      When fragment 1 exists
      And I am the fragment owner
      And I am on the fragment
      And I dumped the response
      # ...

    This allows you to do some debugging on that error you can't fathom out. It helped me greatly when I put in devise authentication and discovered that my code was looping back to the login screen because of a duff password.

    Devise

    I love this plugin - takes away a ton of work

    (Thanks Colin)

    This should get you started with cucumber. Note that password is a helper method that just returns something like 'test123'. My code has user roles (I do it differently from the way the devise guys do it) and I took it out, so it may not run straight away as it's untested. Caveat emptor.

    def create_my_user(params)
      unless user = User.find_by_email(params[:email])
        params[:password_confirmation] = params[:password]
        user = User.create!(params)
        # This makes the user look 'confirmed'
        user.update_attribute(:confirmation_token,nil)
        user.update_attribute(:confirmed_at,Time.now)
      end
      user
    end

    Given /^I am logged in as (.*)$/ do |email|
      @current_user = create_my_user(:email => email, :password => password )
      visit new_user_session_path
      fill_in("Email", :with => email )
      fill_in("Password", :with => password )
      click_button("Sign in")
      response.body.should =~ /My Lovely App/m
    end
     

    Comments (0)

    Add Comment