使用AbstractController :: ActionNotFound的自定义设计会话控制器的RSpec测试失败 [英] RSpec Test of Custom Devise Session Controller Fails with AbstractController::ActionNotFound

查看:167
本文介绍了使用AbstractController :: ActionNotFound的自定义设计会话控制器的RSpec测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用rspec测试一个定制的Devise会话控制器。我的控制器如下所示:

  class SessionsController< Devise :: SessionsController 

def create
#valid email?
if!(params [:email] =〜/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A- Za-z] {2,} $ /)
set_flash_message:notice,请输入有效的电子邮件地址!
end

超级
结束
结束

我的RSpec控制器测试是这样的:

  require'spec_helper'
require'devise / test_helpers'

描述SessionsController do

它应该对无效的邮件地址登录尝试发出警告do
post:create,:user => {:email => 'invalidEmailAddress'}
response.should包含请输入有效的电子邮件地址!
end

它应该对有效的邮件地址登录尝试没有警告do
pending
end
end

如果我执行RSpec测试,它将失败,并显示以下行:

 失败/错误:post:new,:user => {:email => 'invalidEmailAddress'} 
AbstractController :: ActionNotFound
#./spec/controllers/sessions_controller_spec.rb:7

来自plataformatec Devise Wiki的提示以及这个帖子没有解决这个问题。感谢您的帮助。



加号



我进一步调查。我实际上能够通过控制器规范的以下补充来删除错误:

  before(:each)do 
request.env ['devise.mapping'] = Devise.mappings [:user]
end

但是现在出现一个新的错误:

 失败/错误:post:create# 
在此操作中多次调用渲染和/或重定向。请注意,您只能调用呈现或重定向,每次操作最多只能调用一次。还要注意,重定向或渲染都不会终止操作的执行,所以如果要在重定向后退出操作,则需要执行redirect_to(...)并返回。

即使在继承控制器中遗漏了创建方法,也会出现错误。例如,get:new的错误不会出现。它似乎是发布:只创建。
我没有想法?任何帮助?
谢谢!

解决方案

我最后修改了我的问题,包括设计测试帮助者,调用方法setup_controller_for_warden在我的test AND doing request.env [devise.mapping] = Devise.mappings [:user]。像这样:

  require'test_helper'

class SessionsControllerTest< ActionController :: TestCase
include Devise :: TestHelpers

testshould reject invalid captchado
setup_controller_for_warden
request.env [devise.mapping] = Devise .mappings [:user]

get:new

assert_response:success
end
end
/ pre>

不知道你的双渲染问题,你确定你应该调用帖子:创建然后渲染?我不知道rspec应该如何工作。


I am currently trying to test a custom Devise session controller with rspec. My controller looks like this:

class SessionsController < Devise::SessionsController

  def create 
    #valid email?
    if !(params[:email] =~ /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/)
      set_flash_message :notice, "Please enter a valid e-mail address!"
    end

    super
  end
end

My RSpec Controller Test is this:

require 'spec_helper'
require 'devise/test_helpers'

describe SessionsController do  

  it "should put a warning on invalid mail address login attempt" do
    post :create, :user => {:email => 'invalidEmailAddress'}
    response.should contain "Please enter a valid e-mail address!"
  end

  it "should put no warning on valid mail address login attempt" do
    pending
  end
end

If I execute the RSpec Test it fails with the following line:

Failure/Error: post :new, :user => {:email => 'invalidEmailAddress'}
     AbstractController::ActionNotFound
     # ./spec/controllers/sessions_controller_spec.rb:7

Tips from the plataformatec Devise Wiki as well as this post did not solve this issue. Thanks for your help.

Addition

I investigated further. I was actually able to "remove" the error with the following addition to the controller spec:

before(:each) do
  request.env['devise.mapping'] = Devise.mappings[:user]
end

But now a new error appears:

Failure/Error: post :create  #currently fails with multiple render warning
Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".

Even with the create method left out in the inheriting controller the error appears. The error does not appear on get :new for example. It seems to be post :create only. I am out of ideas? Any help? Thanks!

解决方案

I finally fixed my problem by doing including the devise test helpers, calling the method setup_controller_for_warden in my test AND doing request.env["devise.mapping"] = Devise.mappings[:user]. Like so:

require 'test_helper'

class SessionsControllerTest < ActionController::TestCase
    include Devise::TestHelpers

    test "should reject invalid captcha" do
       setup_controller_for_warden
       request.env["devise.mapping"] = Devise.mappings[:user]

       get :new

       assert_response :success
   end
end

Not sure about your double render problem though, are you sure your supposed to call post :create then render? i'm not sure how rspec is supposed to work.

这篇关于使用AbstractController :: ActionNotFound的自定义设计会话控制器的RSpec测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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