2012-01-11 35 views
8

ApplicationController:Rails + rspec + devise = phương thức undefined `authenticate_user! '

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 

Kết quả:

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

Ray phiên bản: 3.1.3

RSpec phiên bản: 2.8.0

lập mưu phiên bản: 1.5.3

Lưu ý: Tôi cũng tạo ra hỗ trợ/file deviser.rb nhưng điều đó không giúp đỡ. Bất kỳ ý tưởng?

+1

[devise Wiki] (https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3- (và-rspec)) cung cấp vài cách khác nhau để tích hợp với rspec. –

Trả lời

12
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 

Cập nhật:

Sử dụng trên cú pháp với rspec mới nhất sẽ cung cấp cho bên dưới cảnh báo

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)>'. 

Sử dụng cú pháp mới này

before do 
    allow(controller).to receive(:authenticate_user!).and_return(true) 
    end 
+0

Tôi tìm thấy dòng lệnh 'controller.stub (: authenticate_user!). And_return true' rất hữu ích. Tuy nhiên, dòng 'include Devise :: TestHelpers' là không cần thiết và gây hiểu nhầm. Không có phương thức nào từ 'Devise :: TestHelpers' được sử dụng ở đây, và một lý do để khai báo' authenticate_user! 'Ngay từ đầu là tránh ghép nối với máy móc của Devise. – evanrmurphy

+1

@evanrmurphy yeap, bạn thật sự đúng, dunno những gì điều này bao gồm làm trong câu trả lời của tôi, tôi sẽ loại bỏ nó :) –

7

Là tên mô hình của bạn một cái gì đó khác hơn Người dùng? Nếu có, ví dụ: Quản trị viên, sau đó bạn cần phải thay đổi bộ lọc của mình thành:

before_filter :authenticate_admin! 

Điều này hơi một chút; Tôi bắt đầu với User như mô hình của mình, và sau đó quyết định thêm Devise vào một mô hình có tên là Member thay vào đó, nhưng tôi đã để lại :authenticate_user! gốc trong bộ điều khiển của mình và tiếp tục nhận lỗi đó khi chạy RSpec.

+0

cảm ơn bạn cảm ơn bạn cảm ơn bạn! –

3

Hình như cách tốt nhất để làm điều này là như sau trong tập tin spec_helper.rb của bạn:

RSpec.configure do |config| 
    config.include Devise::TestHelpers, :type => :controller 
end 

Xem rspec wiki để biết thêm chi tiết.

1

Trong trường hợp của tôi, tôi đã quên rằng tôi đã nhận xét ra dòng devise_for trong tệp routes.rb của tôi.

Các vấn đề liên quan