blog.robotex.de

Devise: Testing a custom sessions controller

Assume you have a custom devise sessions controller for your customer login at Rails.root/app/controllers/customers/sessions_controller.rb:

class Customers::SessionsController < Devise::SessionsController
end

To test it with rspec, you'll have to add a before(:each) to the test:

before(:each) do
  setup_controller_for_warden
  request.env["devise.mapping"] = Devise.mappings[:customer]
end

You're test should look like this:

require 'spec_helper'

describe Customers::SessionsController do
  render_views

  before(:each) do
    setup_controller_for_warden
    request.env["devise.mapping"] = Devise.mappings[:customer]
  end

  context "as a visitor" do
    describe "GET 'sign_in'" do
      it "should have an email field" do
        get :new
        response.should have_selector("input[name='customer[email]']")
      end
      
      it "should have a password field" do
        get :new
        response.should have_selector("input[name='customer[password]']")
      end
    end
  end
end

Back