向 Rails4 应用程序添加授权后,RSpec 控制器规范失败 [英] RSpec controller specs fail after adding authorization to Rails4 application
问题描述
关注 Railscast #385 Ƃ 我已经为我的 Rails 4 应用程序(rails4.2.6,rspec-rails 3.4.2).添加授权后,我所有的控制器规格都失败了.
Following Railscast #385 & #386 I have added authorization to my Rails 4 application (rails 4.2.6, rspec-rails 3.4.2). After adding authorization, all of my controller specs fail.
我的功能规范仍然通过,因为在规范中我以管理员身份登录以执行不允许访问者的操作(:edit、:update...).如果我允许访问者执行所有操作,我的控制器规格就会通过.但是,只有管理员才能执行的任何操作都将在控制器规范中失败.
My feature specs still pass because in the spec I login as admin to perform actions that are not allowed visitors (:edit, :update...). If I make all actions allowed to a visitor my controller specs pass. However, any action that only the admin can perform will fail in the controller spec.
脚手架生成器创建一个空的
The scaffold generator creates an empty
let(:valid_session) { { } }
它在评论中说:
# 这应该返回会话中应该存在的最小值集为了通过 MyController 中定义的任何过滤器(例如身份验证).
# This should return the minimal set of values that should be in the session in order to pass any filters (e.g. authentication) defined in MyController.
但是,我不知道如何配置此 :valid_session 以允许身份验证通过.
However, I don't know how to configure this :valid_session to allow authentication to pass.
当我尝试在这个问题中发布的解决方案时:
When I try the solution posted in this question:
def valid_session
controller.stub!(:signed_in?).and_return(true)
end
我收到此错误:
NoMethodError:
undefined method `stub' for #<MyController
既然逻辑的所有分支路径都应该在较低级别(即控制器规范)而不是功能规范进行测试,那么我如何让控制器规范通过?
Since, all of the many branching paths of logic should be tested at a lower level (ie the controller spec) rather than the feature spec, how do I get the controller specs to pass?
我如何配置 valid_session 或者我可以使用什么其他方法在我的控制器规范中传递授权?
How do I configure valid_session or what other method can I use to pass authorization in my controller specs?
推荐答案
好的,有了这个博客 在向控制器添加授权后,我能够通过控制器测试.
Ok, with the help of this blog I was able to get my controller tests to pass after adding authorization to the controllers.
在 spec/support/helpers/controller_macros.rb 中,我定义了这些方法:
In spec/support/helpers/controller_macros.rb I defined these methods:
module ControllerMacros
def login_admin
user = FactoryGirl.create(:admin)
allow(controller).to receive(:current_user).and_return(user)
end
def login_member
user = FactoryGirl.create(:member)
allow(controller).to receive(:current_user).and_return(user)
end
def login_visitor
allow(controller).to receive(:current_user).and_return(nil)
end
end
要在 spec/support/helpers.rb 中激活这些方法:
To activate those methods in spec/support/helpers.rb:
RSpec.configure do |config|
config.include ControllerMacros, type: :controller
end
然后在控制器规范中实现它,spec/controllers/topics_controller_spec.rb:
And then implement it in the controller spec, spec/controllers/topics_controller_spec.rb:
require 'rails_helper'
RSpec.describe TopicsController, type: :controller do
let(:valid_attributes) {
{ :name => "MyTopic" }
}
let(:invalid_attributes) {
{ :name => nil }
}
before(:each) do
login_admin
end
describe "GET #index" do
it "assigns all topics as @topics" do
login_visitor
topic = Topic.create! valid_attributes
get :index, {}
expect(assigns(:topics)).to eq([topic])
end
end
describe "GET #show" do
it "assigns the requested topic as @topic" do
login_visitor
topic = Topic.create! valid_attributes
get :show, {:id => topic.to_param}
expect(assigns(:topic)).to eq(topic)
end
end
describe "GET #new" do
it "assigns a new topic as @topic" do
get :new, {}
expect(assigns(:topic)).to be_a_new(Topic)
end
end
[...]
end
这将在每次测试前以管理员身份登录.请注意,您应该在具有权限的最低级别测试每个操作,因此以访问者(未登录)身份测试 show 和 index. 如果所有操作都需要管理员权限,则可以使用 before do
而不是 before(:each) do
以节省测试时间.
This will login as admin before each test. Note that you should test each action at the lowest level that has permission, so show and index are tested as a visitor (not-logged in). If all actions need admin permission then you can use before do
instead of before(:each) do
to save a little time in the tests.
最后,您必须从规范中删除所有对 valid_session
的提及.
Finally you must remove all mention of valid_session
from the spec.
这篇关于向 Rails4 应用程序添加授权后,RSpec 控制器规范失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!