“创建后”的问题在我的设计控制器rspec [英] Problem with "post create" in my devise controller rspec

查看:96
本文介绍了“创建后”的问题在我的设计控制器rspec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[确定...我的第一个问题,所以要温柔。]



我正在使用设备进行身份验证,但是我有自己的控制器来扩展发生的情况创建用户时。在注册时注册(注册),我正在创建用户和代理。



在路由...

  devise_for:users,:controllers => {:registrations => 注册} 

我的完整控制器...

  class RegistrationsController< Devise :: RegistrationsController 
def create
super#创建@user
@agency = Agency.create! params [:agency]
@ agency.users<<< @user
@ agency.owner = @user
@ user.agency = @agency
@ agency.save
@ user.account_admin = true
@ user.save
end
end

我的问题是我要设置一个rspec检查这个代码。该代码似乎正在工作,但我正在为我的规格中的100%的代码覆盖率进行拍摄。这是我的整个规范...

  require'spec_helper'
描述RegistrationsController do
render_views
描述POST创建do
它创建一个关联的用户do
@agency = Factory.create(:agency)
@user = Factory.create(:user,:agency => @agency)
User.stub(:new).with({'name'=>'pat'}){@user}
Agency.stub(:new).with {'name'=>'pat'}){@agency}
post:create,:user => {'name'=> 'pat'}
assigns(:user).should be(@user)
end
end
end

但是,我在post create发生错误。以下是错误消息

 找不到路径/ users的设计映射?user [name] = pat

这是(我认为)耙路的相关行

  user_registration POST /users(.:format){:action =>创建,:控制器=>注册} 

任何想法?



pat

解决方案

具体来说,从@shanethehat引用的链接中复制一个代码段,

  @ request.env [devise.mapping] = Devise.mappings [:admin] 

解决了问题。只需将该行放入 before_filter 中即可进行Devise风味控制器测试。将:admin 更改为有问题的资源(通常:user



工作原因:单独描述 SessionsController 不能唯一标识特定的Devise资源。例如,如果您的应用程序具有 admin 用户资源,则可能希望有2个 SessionsController (含)2套规格 - 每种资源类型一种。在这种情况下,对于每个测试来操作正确的资源,您需要告诉Devise哪个 SessionsController

[Ok... my first question, so be gentle.]

I am using devise for my authentication, but I have my own controller to extend what happens when the user is created. I am creating both a "user" and an "agency" at the time of the registration (sign up).

In routes...

 devise_for :users, :controllers => {:registrations => "registrations"}

My complete controller...

 class RegistrationsController < Devise::RegistrationsController
   def create
     super # creates the @user
     @agency = Agency.create! params[:agency]
     @agency.users << @user
     @agency.owner = @user
     @user.agency = @agency
     @agency.save
     @user.account_admin = true
     @user.save
   end
 end

My problem is that I want to set up an rspec to check this code. The code seems to be working, but I am shooting for 100% code coverage in my specs. Here is my entire spec...

 require 'spec_helper'
 describe RegistrationsController do
   render_views  
   describe "POST create" do
     it "creates an associated user" do
       @agency = Factory.create( :agency )
       @user = Factory.create( :user, :agency => @agency )
       User.stub(:new).with({'name' => 'pat'}) { @user }
       Agency.stub(:new).with({'name' => 'pat'}) { @agency }
       post :create, :user => {'name' => 'pat'}
       assigns(:user).should be(@user)
     end
   end
 end

But, I am getting an error on the "post create". Here is the error message

 Could not find devise mapping for path "/users?user[name]=pat"

And this is (I think) the relevant line from "rake routes"

 user_registration POST   /users(.:format)  {:action=>"create",:controller=>"registrations"}

Any thoughts?

pat

解决方案

Specifically, copying out a snippet from the link referenced by @shanethehat, the line

@request.env["devise.mapping"] = Devise.mappings[:admin]

solves the problem posed in the question. Just drop that line into a before_filter for your Devise-flavored controller test. Change :admin to the resource in question (commonly :user)

The reason that is works: Describing a SessionsController alone does not uniquely identify a specific Devise resource. For example, if your app has admin and user resources, you might like to have 2 SessionsController(s) with 2 sets of specs - one for each resource type.

In that case, for each test to operate on the right resource, you need to tell Devise which of your SessionsController(s) you mean for each set of examples. The line above does just that.

这篇关于“创建后”的问题在我的设计控制器rspec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆