测试Rails 3.1模型创建时的ActionDispatch :: ClosedError(RSpec / Cucumber)(ActionDispatch::ClosedError when te

编程入门 行业动态 更新时间:2024-10-09 07:28:34
测试Rails 3.1模型创建时的ActionDispatch :: ClosedError(RSpec / Cucumber)(ActionDispatch::ClosedError when testing Rails 3.1 model creation (RSpec/Cucumber))

我正在用Ruby on Rails 3.1(RC1)创建一个Web应用程序。 我正在使用Factory GirlRSpecCucumber (与Capybara )进行测试,但是当我创建新用户时(通过用户模型的创建操作),我遇到意外提出的ActionDispatch::ClosedError s某些时间(并非每次) 。 以下是我得到的错误消息:

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'do get:edit,id:user.id#这会引发无错分配[:user] .should ==用户结束结束

在此之下的任何其他方法都不会引发错误,即使我以完全相同的方式创建用户。

任何建议,我可能做错了将不胜感激!

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!

最满意答案

这是由于rails 3现在响应的方式。 他们针对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

更多推荐

本文发布于:2023-07-28 03:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1299850.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   测试   ActionDispatch   ClosedError   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!