2016-06-11 13 views
0

ich ersinnen bin mit und möchte meine Spezifikationen überprüfen, die bestimmte Controller-Aktionen durch abgedeckt sind authenticate_user!Benutzer für Aktionen authentifiziert Überprüfen

Ich habe die devise docs sah über, ich fühle, es ist etwas wirklich einfach und offensichtlich, dass ich Ich komme nicht auf die Art und Weise, wie das Gerät funktioniert.

Basiert auf this SO post Ich baute eine Spezifikation, um dies zu überprüfen, aber es scheint nicht zu funktionieren.

Wenn ich die spec laufen, erhalte ich:

Failure/Error: expect(controller).to receive(:authenticate_user!) 

    (#<LibrariesController:0x000001035639f8>).authenticate_user!(*(any args)) 
     expected: 1 time with any arguments 
     received: 0 times with any arguments 

libraries_controller_spec.rb

require 'rails_helper' 

RSpec.describe LibrariesController, type: :controller do 

    let(:valid_attributes) { 
    skip("Add a hash of attributes valid for your model") 
    } 

    let(:invalid_attributes) { 
    skip("Add a hash of attributes invalid for your model") 
    } 

    let(:valid_session) { {} } 

    describe "GET #index" do 
    it "should be authenticated" do 
     get :index, {} 
     expect(controller).to receive(:authenticate_user!) 
    end 
    end 
end 

Und der Controller selbst:

libraries_controller.rb

class LibrariesController < ApplicationController 
    before_action :set_library, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user! 

    # GET /libraries 
    # GET /libraries.json 
    def index 
    @libraries = Library.all 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_library 
     @library = Library.find(params[:id]) 
    end 
end 

Antwort

1

Nun, das war peinlich. Es stellte sich heraus, dass ich den Erwartungs- und Methodenaufruf in meiner Spezifikation in der falschen Reihenfolge hatte. Es sollte sein:

describe "GET #index" do 
    it "should be authenticated" do 
    expect(controller).to receive(:authenticate_user!) 
    get :index, {} 
    end 
end 
Verwandte Themen