我正在使用设计并希望检查我的规范,authenticate_user!
我查看了设计文档,我觉得有一些非常简单明了的事情,我没有想到设计的方式。
基于this SO post我制作了一个规范来检查这个,但它似乎没有用。
当我运行规范时,我得到:
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
控制器本身:
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
答案 0 :(得分:1)
那很令人尴尬。事实证明我在我的规范中以错误的顺序进行了期望和方法调用。它应该是:
describe "GET #index" do
it "should be authenticated" do
expect(controller).to receive(:authenticate_user!)
get :index, {}
end
end