【发布时间】:2011-05-30 18:51:38
【问题描述】:
我正在使用 Ruby on Rails 3.1 (RC1) 创建一个 Web 应用程序。我正在使用 Factory Girl、RSpec 和 Cucumber(与 Capybara)进行测试,但我遇到了意外的提升ActionDispatch::ClosedErrors 有时(不是每次)当我创建新用户时(通过 User 模型的 create 操作)。以下是我收到的错误消息:
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编辑'做 它'将请求的用户分配为@user' get :edit, id: user.id # 这不会引发错误 分配[:user].should == user 结尾 结束
下面的任何其他方法都不会引发错误,即使我以完全相同的方式创建用户。
对我可能做错的任何建议将不胜感激!
【问题讨论】:
标签: cucumber rspec2 factory-bot ruby-on-rails-3.1 actiondispatch