我正在尝试转移到 rspec 进行测试,但我无法让 Controller 测试与用户身份验证一起使用。测试不需要用户的路由。
require 'spec_helper'
describe UddsController do
include Devise::TestHelpers
render_views
before (:each) do
@user = User.new(:email => "test@user.com", :username => "test123")
@user.roles << Role.find_or_create_by_name("admin")
@user.save
sign_in @user
end
it "should get index" do
get :index
response.should be_success
end
刚好
2) UddsController should get index
Failure/Error: response.should be_success
expected success? to return true, got false
# ./spec/controllers/udds_controller_spec.rb:21
我没有使用 factory_girl。此处和谷歌上的所有示例修复似乎都是如此。我不明白为什么我所做的不起作用。错误在调试中没有用。
我知道这个问题已经被问死了。我已经阅读了所有我能找到的博客和论坛。我走投无路了。
最佳答案
它可能正在重定向。 be_success
对任何非 2xx 状态代码(如 302)返回 false。尝试检查 response.status
的值——如果它正在重定向,则可能意味着您的身份验证或授权方案产生了意外结果。
关于ruby-on-rails-3 - Rspec 2 + Devise + Cancan - Factorygirl = Controller 测试不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6360835/