我目前正在尝试用rspec测试一个定制的device会话控制器。我的控制器如下:
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
平台上的提示设计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:只创建。
我没主意了?有什么帮助吗?
谢谢!