加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails rspec devise =未定义的方法`authentica

发布时间:2020-12-16 19:32:37 所属栏目:百科 来源:网络整理
导读:ApplicationController中: class ApplicationController ActionController::Base before_filter :authenticate_user! protect_from_forgeryend DashboardsController: class DashboardsController ApplicationController def index endend DashboardsContr
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读