3

我正在用Ruby on Rails 3.1(RC1)创建一个Web应用程序。我使用工厂女孩RSpec的黄瓜(与水豚)进行测试,但我遇到意外上调ActionDispatch::ClosedError s的照片时(不是每个时间),当我创建新用户(通过的用户模型的创建操作)。下面是我得到的错误信息:当测试Rails 3.1模型创建(RSpec/Cucumber)时,ActionDispatch :: ClosedError

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

的错误是创建用户的这些方式时提出:利用工厂女孩

  • Factory.create(:user)
  • Factory.build(:user).save

  • 创作
  • 基本创作
    • User.create({ ... })
    • User.new({ ... }).save

什么是有趣的是,他们的一些测试期间的工作,但不是别人,它似乎并没有随机的,虽然我不能找出原因。下面是从我的代码的摘录:

users_controller_spec.rb

需要 '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 

但是,错误并不在下面的代码块中引发:

描述'GET edit'do 它'将请求的用户分配为@user'do get:edit,id:user.id#这不会产生错误 分配[:user] .should ==用户 结束 结束

以下任何其他方法都不会引发错误,即使我以完全相同的方式创建用户。

对于我可能会做错的任何建议将不胜感激!

回答

2

这是由于rails 3现在响应的方式。他们针对Flash中的相同问题发布了修复方案,但尚未在Cookie中发布。现在我已经关闭了我的请求规格。如果在此之前没有人接触到这个问题,我将在本周末看看这个问题。

https://github.com/rails/rails/issues/1452

+0

谢谢,这解释了错误。我希望尽快修复cookies,以便我的规格正确运行。 – sindrenm 2011-06-02 10:39:52

1

正是这样,我们就不必跟随链接,这是我的authlogic变通办法的修改后的版本:

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

如果我正在测试,我不会在每个.save调用中确保会话管理,而只是关闭它们。