gpt4 book ai didi

ruby-on-rails - Rails + RSpec + devise =未定义方法 `authenticate_user!'

转载 作者:行者123 更新时间:2023-12-04 04:46:16 24 4
gpt4 key购买 nike

ApplicationController:

class ApplicationController < ActionController::Base
before_filter :authenticate_user!

protect_from_forgery
end

DashboardsController:
class DashboardsController < ApplicationController
def index
end

end

DashboardsControllerSpec:
require 'spec_helper'
describe DashboardsController do
include Devise::TestHelpers

describe "GET 'index'" do
it "returns http success" do
get 'index'
response.should be_success
end
end
end

结果:
Failure/Error: get 'index'
NoMethodError:
undefined method `authenticate_user!' for #<DashboardsController:0x007fef81f2efb8>

Rails版本:3.1.3

Rspec版本:2.8.0

设计版本:1.5.3

注意:我还创建了support/deviser.rb文件,但这无济于事。有任何想法吗?

最佳答案

require 'spec_helper'
describe DashboardsController do
before { controller.stub(:authenticate_user!).and_return true }
describe "GET 'index'" do
it "returns http success" do
get 'index'
response.should be_success
end
end
end

更新:

在最新的rspec中使用以上语法将给出以下警告
Using `stub` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead. Called from  `block (2 levels) in <top (required)>'.

使用这个新语法
  before do
allow(controller).to receive(:authenticate_user!).and_return(true)
end

关于ruby-on-rails - Rails + RSpec + devise =未定义方法 `authenticate_user!',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8819343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com