ActionDispatch :: ClosedError when testing Rails 3.1 model creation (RSpec / Cucumber)

I am building a web application with Ruby on Rails 3.1 (RC1). I use Factory Girl, RSpec and Cucumber (with Capybara) for testing, but I experience an unexpected uplift ActionDispatch::ClosedErrorat some points (not every time) when I create new users (by creating the User model). The following is the error message:

Cannot modify cookies because it was closed. This means it was already streamed
back to the client or converted to HTTP headers. (ActionDispatch::ClosedError)

Error using these methods to create users:

  • Creating with Factory Girl
    • Factory.create( :user )
    • Factory.build( :user ).save
  • Base creation
    • User.create( { ... } )
    • User.new( { ... } ).save

It's funny that they work during some test, but not in others, and this does not seem random, although I can not understand the reason. Below is a snippet of my code:

users_controller_spec.rb

  require 'spec_helper'

def user
  @user ||= Factory.create( :user )
end

def valid_attributes
  Factory.attributes_for :user
end

describe UsersController do

  describe 'GET index' do
    it 'assigns all users as @users' do
      users = [ user ] # The call to user() raises the error here
      get :index
      assigns[ :users ].should == users
    end
  end

  describe 'GET show' do
    it 'assigns the requested user as @user' do
      get :show, id: user.id # The call to user() raises the error here
      assigns[ :user ].should == user
    end
  end

However, the error does not occur in the following code block:

   "GET edit" do      @user 'do       get: edit, id: user.id #        [: user].should == user        

, , , .

, , !

+3
3
+2

Just so we don't follow the links, here is my modified version of the authlogic workaround:

class User < ActiveRecord::Base
  acts_as_authentic do |c|
    c.maintain_sessions = false if Rails.env == "test"
  end    
end

Instead of having to provide session management for every .save call, I will simply disable them if I test.

+1
source

All Articles