Project

caulfield

0.0
No commit activity in last 3 years
No release in over 3 years
A Rack middleware to aid in integration testing for Rails 3 and up.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Caulfield

Caulfield is a small Rack middleware to be used in integration testing for Rails 3 applications. It offers a simple interface to inspect the session and cookie jar of the most recent request, and to set values and cookies for the next request. I'm using it with Capybara and RSpec, but I don't see why it wouldn't work with any other integration testing setup. It's a simple extraction from one of my projects, where I use it extensively, but it itself doesn't have tests, so beware.

If you're using Rails 3.0.0.beta4 or higher, Caulfield will insert itself into your middleware stack automatically. Otherwise, you'll have to specifically use Caulfield::Middleware.

How to Use

  • Run Caulfield.reset before each test. You can do this globally - for example, in your spec_helper file in RSpec:

      Rspec.configure do |config|
        config.before :each do
          Caulfield.reset
        end
      end
    
  • After you've processed a request, you can access its session and cookie jar using Caulfield.session and Caulfield.cookies, respectively.

  • You can set session values and cookies for the next request using Caulfield.set_session and Caulfield.set_cookies, each of which take a hash.

  • To remove a value from the session or cookie jar, set the value to nil.

You can wrap whatever DSL you like around these methods. For example:

def login_as(user)
  Caulfield.set_session :user_id => user.id
end

def logged_in?
  !!Caulfield.session[:user_id]
end

def logout
  Caulfield.set_session :user_id => nil
end

In addition to the session and cookies from the request, you can also use:

  • Caulfield.request => The ActionDispatch::Request object from the last request.
  • Caulfield.env => The env hash from the last request
  • Caulfield.status, Caulfield.headers, Caulfield.body => The components of the app's last Rack response.

Happy testing!