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

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

问题描述

[好吧...我的第一个问题,所以要温柔.]

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

我使用 devise 进行身份验证,但我有自己的控制器来扩展创建用户时发生的事情.我在注册(注册)时创建了一个用户"和一个代理".

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).

在路线...

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

我的完整控制器...

 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

我的问题是我想设置一个 rspec 来检查此代码.代码似乎可以正常工作,但我希望在我的规范中实现 100% 的代码覆盖率.这是我的整个规格...

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"

这是(我认为)来自rake routes"的相关行

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

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

有什么想法吗?

推荐答案

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

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

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

解决问题中提出的问题.只需将该行放到 before_filter 中,即可进行设计风格的控制器测试.将 :admin 更改为相关资源(通常为 :user)

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)

有效的原因:单独描述 SessionsController 并不能唯一标识特定的设计资源.例如,如果您的应用程序具有 adminuser 资源,您可能希望有 2 个 SessionsController(s) 和 2 组规范 - 一个对于每种资源类型.

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.

在这种情况下,对于在正确的资源上运行的每个测试,您需要告诉 Devise 对于每组示例,您指的是哪个 SessionsController(s).上面的行就是这样做的.

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天全站免登陆