在轨道上设计1.2红宝石
我在测试注册时遇到了困难。当用户点击注册时,他们已经登录,我应该看到一条flash消息。这有效但我的测试失败了。不知道为什么。注册如何工作?是否有某种内部重定向发生?这一步失败:
Then I should see "You have registered successfully. If enabled, a confirmation was sent your e-mail."
我的用户模型未启用确认。
答案 0 :(得分:5)
在某种程度上,您不应该觉得需要对设计机制进行单元测试 - 宝石本身已经过充分测试。我可以理解想要确保它的行为与你配置它的方式相同,所以:
设计成功后,设计肯定会重定向。它将设置flash消息,然后重定向到您在路由文件中设置为root的内容,或者如果您尝试访问站点内的页面并重定向到登录页面,它会将您重定向回您的页面试图访问。
对于您的测试,请尝试测试您将被重定向到您在routes.rb fil中设置为root的内容。即在设计说明中,它说它设置为
root :to => "home#index"
所以,在你的测试中尝试这样的事情:
require 'spec_helper'
describe YourController do
include Devise::TestHelpers
before (:each) do
@user = Factory.create(:user)
sign_in @user
end
describe "GET 'index'" do
it "should be successful" do
get 'index'
response.should be_success
end
it "should redirect to root" do
get 'index'
response.should redirect_to(root_url)
end
end
您也可以将Flash消息测试添加到此。希望这有帮助!