问题描述
|
我正在使用Ruby on Rails 3.1(RC1)创建一个Web应用程序。我正在使用Factory Girl,RSpec和Cucumber(与Capybara一起)进行测试,但是在我通过用户模型的create动作创建新用户时,有时(并非每次)都遇到意外的raised0升高。以下是我收到的错误消息:
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 \'做
'将请求的用户分配为@user \'
get:edit,id:user.id#这不会引起错误
Assigns [:user]。应该== user
结束
结束
即使我以完全相同的方式创建用户,在此之下的任何其他方法也不会引发错误。
对于我可能做错的任何建议将不胜感激!
解决方法
这是由于导轨3现在传输响应的方式。他们在Flash中针对同一问题发布了边缘修复程序,但在Cookie中尚未发布。目前,我已经关闭了我的请求规范。如果没有人在这个周末之前,我将在这个周末看这个问题。
https://github.com/rails/rails/issues/1452
, 有人在这里发布了解决方法
https://github.com/binarylogic/authlogic/issues/262#issuecomment-1804988
, 只是我们不必遵循链接,这是我对authlogic解决方法的修改版本:
class User < ActiveRecord::Base
acts_as_authentic do |c|
c.maintain_sessions = false if Rails.env == \"test\"
end
end
如果要进行测试,我只是将其关闭,而不是负责确保每个.save调用的会话管理。