我正在使用 Ruby on Rails 3.1 (RC1) 创建一个 Web 应用程序。我正在使用Factory Girl、RSpec和Cucumber(与Capybara )进行测试,但是当我创建新用户(通过 User 模型的创建操作)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
但是,以下代码块中未引发错误:
describe 'GET edit' do it 'assigns the requested user as @user' do get :edit, id: user.id # 这不会引发错误 assigns[ :user ].should == user end end
即使我以完全相同的方式创建用户,低于此的任何其他方法都不会引发错误。
任何关于我可能做错的建议将不胜感激!