Ruby on rails 如何在控制器规范中禁用before_操作?
我在我的控制器规范中使用了这个:Ruby on rails 如何在控制器规范中禁用before_操作?,ruby-on-rails,ruby-on-rails-4,rspec,rspec-rails,Ruby On Rails,Ruby On Rails 4,Rspec,Rspec Rails,我在我的控制器规范中使用了这个: controller.class.skip_before_action 具体而言,在这种情况下: controller.class.skip\u在采取行动之前:要求\u授权\u查看\u材料 物料控制员: class MaterialsController < ApplicationController before_action :set_material, only: [:show, :edit, :update, :destroy] befo
controller.class.skip_before_action
具体而言,在这种情况下:
controller.class.skip\u在采取行动之前:要求\u授权\u查看\u材料
物料控制员:
class MaterialsController < ApplicationController
before_action :set_material, only: [:show, :edit, :update, :destroy]
before_action :require_authorisation_to_view_materials, only: [:index, :show]
def require_authorisation_to_view_materials # For Materials Page
unless user_signed_in? && current_user.can_view_materials?
redirect_to root_path, alert: "You are not authorised to view the Materials page."
end
end
# GET /materials
# GET /materials.json
def index
@materials = Material.all
end
# GET /materials/1
# GET /materials/1.json
def show
end
# GET /materials/new
def new
@material = Material.new
end
# GET /materials/1/edit
def edit
end
# POST /materials
# POST /materials.json
def create
@material = Material.new(material_params)
respond_to do |format|
if @material.save
format.html { redirect_to materials_path, notice: 'Material was successfully created.' }
format.json { render :show, status: :created, location: @material }
else
format.html { render :new }
format.json { render json: @material.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /materials/1
# PATCH/PUT /materials/1.json
def update
respond_to do |format|
if @material.update(material_params)
format.html { redirect_to materials_path, notice: 'Material was successfully updated.' }
format.json { render :show, status: :ok, location: @material }
else
format.html { render :edit }
format.json { render json: @material.errors, status: :unprocessable_entity }
end
end
end
# DELETE /materials/1
# DELETE /materials/1.json
def destroy
@material.destroy
respond_to do |format|
format.html { redirect_to materials_url, notice: 'Material was successfully deleted.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_material
@material = Material.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def material_params
params.require(:material).permit(:title, :level, :description, :link)
end
end
你能看出这有什么问题吗?我真的不明白
controller.class.skip\u在采取行动之前:要求\u授权\u查看\u材料
很好用,我以前在使用它的时候遇到过一些奇怪的事情(但我不确定这是不是罪魁祸首)。有人能解释一下这句话的确切含义吗?如果我的
controller.class.before_action :require_authorisation_to_view_materials
实际上是否具有在物料控制器中“重新打开”之前“切换”操作的预期效果?我的规范代码看起来正常吗?在执行控制器规范和伪造登录时,我喜欢使用期望值来替代授权 i、 e.在你的情况下:
require "rails_helper.rb"
describe MaterialsController do
before :each do
allow(controller).to receive(:require_authorisation_to_view_materials).and_return(true)
end
#..snip
end
甚至更好
require "rails_helper.rb"
describe MaterialsController do
before :each do
allow(controller).to receive(:current_user).and_return(FactoryGirl.create(:admin_user)
end
#..snip
end
所以,因为这是一个控制器规范,我不能像在capybara那样登录用户?或者使用Desive助手(但实际上我真的不想走这条路,除非我不得不这么做!Desive有时会让人头疼得厉害)。我在哪里可以找到您的解决方案的相关文档?您不能像在capybara中那样登录。Capybara用于响应周期测试,而控制器规范仅用于控制器。这里有一些使用stubing登录的例子,尽管这是使用老式的rspec mocking。谢谢。它工作得很好。接下来,我研究了如何模拟登录用户。读了这篇文章后,这一切都是有意义的。测试正在运行sweeeeet!
require "rails_helper.rb"
describe MaterialsController do
before :each do
allow(controller).to receive(:current_user).and_return(FactoryGirl.create(:admin_user)
end
#..snip
end