测试 Rails 3.1 模型创建时出现 ActionDispatch::ClosedError (RSpec/Cucumber)
我正在使用 Ruby on Rails 3.1 (RC1) 创建一个 Web 应用程序。我正在使用 Factory Girl、RSpec 和 Cucumber(与 Capybara)进行测试,但我遇到了意外的问题当我创建新用户(通过用户模型的create操作)时,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 Girl 创建
Factory.create(:user)
Factory.build(:user).save
- 基本创建
User.create( { ... } )
User.new( { ... } ).save
有趣的是,它们在某些时候确实起作用测试,但不是在其他人中进行的,而且它似乎不是随机的,尽管我无法弄清楚原因。以下是我的代码的摘录:
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
但是,以下代码块中不会引发错误:
描述“获取编辑”做什么 它“将请求的用户分配为@user” get :edit, id: user.id # 这不会引发错误 分配[ :user ].should == user 结尾 。
即使我以完全相同的方式创建用户,以下任何其他方法都不会引发错误
任何对我可能做错的事情的建议将不胜感激!
I am creating a web application with Ruby on Rails 3.1 (RC1). I am using Factory Girl, RSpec and Cucumber (with Capybara) for testing, but I am experiencing unexpected raised ActionDispatch::ClosedError
s some of the times (not every time) when I am creating new users (through the User model's create action). Below is the error message that I get:
Cannot modify cookies because it was closed. This means it was already streamed
back to the client or converted to HTTP headers. (ActionDispatch::ClosedError)
The error is raised when using these ways of creating users:
- Creation using Factory Girl
Factory.create( :user )
Factory.build( :user ).save
- Basic creation
User.create( { ... } )
User.new( { ... } ).save
What is funny is that they do work during some test, but not in others, and it does not seem random, although I cannot figure out the reason. Below is an excerpt from 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 is not raised in the following code block:
describe 'GET edit' do
it 'assigns the requested user as @user' do
get :edit, id: user.id # This raises no error
assigns[ :user ].should == user
end
end
Any other method below this does not raise the error, even though I am creating users in the exact same way.
Any suggestions to what I might be doing wrong would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有人在这里发布了解决方法
https://github.com/binarylogic/authlogic/issues/262#issuecomment-1804988
Someone posted a workaround here
https://github.com/binarylogic/authlogic/issues/262#issuecomment-1804988
这是由于 Rails 3 现在传输响应的方式造成的。他们在 Edge 中发布了针对 Flash 中相同问题的修复,但尚未在 Cookie 中修复。现在我已经关闭了我的请求规范。如果在此之前没有人解决这个问题,我将在本周末查看该问题。
https://github.com/rails/rails/issues/1452
This is due to the way rails 3 streams the response now. They posted a fix in edge for the same issue in flash but not in cookies yet. For now I have turned off my request specs. I am going to look at the problem this weekend if no one gets to it before then.
https://github.com/rails/rails/issues/1452
只是为了让我们不必遵循链接,这是我修改后的 authlogic 解决方法版本:
我不是在每个 .save 调用上确保会话管理,而是在测试时将其关闭。
Just so we don't have to follow links, here's my modified version of the authlogic workaround:
Rather than deal with ensuring session management on every .save call, I just turn them off if I'm testing.