Aug 02 2011

Testing Some Devise Features With RSpec, Steak and Email Spec

RSpec Ruby Steak Email Devise | comments

Having spent hours writing cucumber tests and finding out that it all doesn't work with Rails 3.1 RC5 is frustrating. It disrupts the flow of things. In my mind all I wanted was to move on to focus on new features. Not being able to write tests for the new features because of incompatibility issues is just hell. That's the only reason why I started using Steak yesterday for acceptance tests.

But now I found at least 2 more reasons why I shouldn't be using cucumber:

1) We communicate ideas and stories in the language and order we want to. By that I mean we speak usual conversational English. There is no reason to read specific user stories that use a lot of "and" and "then." This is something my clients simply DON'T appreciate.

2) Steak is a lot more simple. Less verbose and therefore some time should be saved.

3) If I work for myself and I am a programmer, why should I ever want to read cucumber stories? I see no need for it.

Getting a Rails 3 .1 app working with Steak is fairly simple if you follow the docs on github. Just update rspec.rake to make sure "rake spec:acceptance" works.

Click here to check the update.

Updated spec/acceptance/acceptance_helper.rb

              require 'spec_helper'
              require 'email_spec'
              
              # Put your acceptance spec helpers inside spec/acceptance/support
              Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
              

Updated spec/acceptance/support/helpers.rb

My RSpec config file looks a bit like this:

              
              RSpec.configure do |config|
                config.include Devise::TestHelpers, :type => :controller
                config.include Warden::Test::Helpers, :type => :acceptance
                config.after(:each, :type => :acceptance) { Warden.test_reset! }
                config.include EmailSpec::Helpers
                config.include EmailSpec::Matchers
              end
              

Here's how to reset password feature (Devise defaults have not changed). I've removed other tests relevant to password reset and just kept one which shows how to test emails.

              require 'acceptance/acceptance_helper'
              
              feature 'Password Reset' do
              
                background do
                  @user = Factory(:user, :email=>"ako@sikatz.com") 
                end
                  
                scenario 'user should receive an email and successfully reset password' do
                  
                  reset_mailer
              
                  visit '/new_password'
                  fill_in "user[email]", :with=>@user.email
                  click_button "Submit"
                  
                  unread_emails_for(@user.email).size.should >= parse_email_count(1)
                  open_email(@user.email)
                  email_should_have_body("Someone has requested a link to change your password, and you can do this through the link below.")
                  click_first_link_in_email
              
                  within('body') do
                    page.should have_content('Change Your Password')
                  end
              
                  fill_in "user[password]", :with=>"password"
                  fill_in "user[password_confirmation]", :with=>"password"
                  click_button "Change Password"
              
                  within('body') do
                    page.should have_content('Your password was changed successfully.')
                  end
                     
                end
                
              end