【问题标题】:ActionDispatch::ClosedError when testing Rails 3.1 model creation (RSpec/Cucumber)测试 Rails 3.1 模型创建时的 ActionDispatch::ClosedError (RSpec/Cucumber)
【发布时间】:2011-05-30 18:51:38
【问题描述】:

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


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      这是由于 rails 3 现在流式传输响应的方式。他们在边缘发布了针对 Flash 中相同问题的修复,但还没有在 cookie 中。现在我已经关闭了我的请求规范。如果没有人在此之前解决这个问题,我将在这个周末查看问题。

      https://github.com/rails/rails/issues/1452

      【讨论】:

      • 谢谢,这解释了错误。我希望尽快修复 cookie,以便我的规范正常运行。
      【解决方案3】:

      只是为了让我们不必关注链接,这是我修改后的 authlogic 解决方法:

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

      我没有处理确保每次 .save 调用的会话管理,而是在测试时将其关闭。

      【讨论】:

        猜你喜欢
        • 2011-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-27
        相关资源
        最近更新 更多