【发布时间】:2010-11-27 12:58:52
【问题描述】:
我目前正在尝试使用 rspec 测试自定义设计会话控制器。我的控制器如下所示:
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
我的 RSpec 控制器测试是这样的:
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
如果我执行 RSpec 测试,它会失败并显示以下行:
Failure/Error: post :new, :user => {:email => 'invalidEmailAddress'}
AbstractController::ActionNotFound
# ./spec/controllers/sessions_controller_spec.rb:7
来自 plataformatec Devise Wiki 以及 this post 的提示并未解决此问题。感谢您的帮助。
加法
我进一步调查。我实际上能够通过对控制器规范的以下添加来“消除”错误:
before(:each) do
request.env['devise.mapping'] = Devise.mappings[:user]
end
但是现在出现了一个新的错误:
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".
即使在继承控制器中省略了 create 方法,也会出现错误。例如,错误不会出现在 get :new 上。它似乎是 post :create only。 我没有想法?有什么帮助吗? 谢谢!
【问题讨论】:
标签: ruby-on-rails controller rspec devise