Reputation: 2802
I'm using devise and want to check in my specs that certain controller actions are covered by authenticate_user!
I've looked over the devise docs, I feel there's something really simple and obvious I'm not getting about the way devise works.
Based on this SO post I built a spec to check this, but it doesn't seem to be working.
When I run the spec, I get:
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
And the controller itself:
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
Upvotes: 0
Views: 46
Reputation: 2802
Well that was embarassing. Turns out I had the expectation and method call in the wrong order in my spec. It should've been:
describe "GET #index" do
it "should be authenticated" do
expect(controller).to receive(:authenticate_user!)
get :index, {}
end
end
Upvotes: 1