测试 Rails 3.1 模型创建时出现 ActionDispatch::ClosedError (RSpec/Cucumber)

发布于 2024-11-10 12:32:20 字数 1662 浏览 3 评论 0原文

我正在使用 Ruby on Rails 3.1 (RC1) 创建一个 Web 应用程序。我正在使用 Factory GirlRSpecCucumber(与 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::ClosedErrors 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Spring初心 2024-11-17 12:32:20

这是由于 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

一梦等七年七年为一梦 2024-11-17 12:32:20

只是为了让我们不必遵循链接,这是我修改后的 authlogic 解决方法版本:

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

我不是在每个 .save 调用上确保会话管理,而是在测试时将其关闭。

Just so we don't have to follow links, here's my modified version of the authlogic workaround:

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

Rather than deal with ensuring session management on every .save call, I just turn them off if I'm testing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文