如何使用 RSpec 正确测试 CanCan 功能
我第一次测试 CanCan 的能力,被难住了。我错过了一些东西......即使我在 can :invite_to 块内返回 false/true,我仍然没有获得通过的规格。我是否缺少使用 CanCan 匹配器?或存根?或者在 CanCan 中定义能力?
我缺少什么吗?
ability.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
can :invite_to, Network do |network|
network.allows_invitations? && (user.admin? || user.can_send_invitations_for?(network))
end
end
end
ability_spec.rb
require 'cancan'
require 'cancan/matchers'
require_relative '../../app/models/ability.rb'
class Network; end;
describe Ability do
let(:ability) { Ability.new(@user) }
describe "#invite_to, Network" do
context "when network level invitations are enabled" do
let(:network) { stub(allows_invitations?: true) }
it "allows an admin" do
@user = stub(admin?: true)
ability.should be_able_to(:invite_to, network)
end
it "allows a member if the member's invitation privileges are enabled" do
@user = stub(admin?: false, can_send_invitations_for?: true)
ability.should be_able_to(:invite_to, network)
end
it "denies a member if the member's invitation privileges are disabled" do
@user = stub(admin?: false, can_send_invitations_for?: false)
ability.should_not be_able_to(:invite_to, network)
end
end
end
end
失败
1) Ability#invite_to, Network when network level invitations are enabled allows an admin
Failure/Error: ability.should be_able_to(:invite_to, network)
expected to be able to :invite_to #<RSpec::Mocks::Mock:0x3fe3ed90444c @name=nil>
# ./spec/models/ability_spec.rb:16:in `block (4 levels) in <top (required)>'
2) Ability#invite_to, Network when network level invitations are enabled allows a member if the member's invitation privileges are enabled
Failure/Error: ability.should be_able_to(:invite_to, network)
expected to be able to :invite_to #<RSpec::Mocks::Mock:0x3fe3edc27408 @name=nil>
# ./spec/models/ability_spec.rb:21:in `block (4 levels) in <top (required)>'
I am testing CanCan abilities for the first time and am stumped. I'm missing something...even if I return false/true inside of the can :invite_to block I am still not getting passing specs. Am I missing using the CanCan matchers? or stubs? or definiing abilities in CanCan?
Anything I'm missing?
ability.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
can :invite_to, Network do |network|
network.allows_invitations? && (user.admin? || user.can_send_invitations_for?(network))
end
end
end
ability_spec.rb
require 'cancan'
require 'cancan/matchers'
require_relative '../../app/models/ability.rb'
class Network; end;
describe Ability do
let(:ability) { Ability.new(@user) }
describe "#invite_to, Network" do
context "when network level invitations are enabled" do
let(:network) { stub(allows_invitations?: true) }
it "allows an admin" do
@user = stub(admin?: true)
ability.should be_able_to(:invite_to, network)
end
it "allows a member if the member's invitation privileges are enabled" do
@user = stub(admin?: false, can_send_invitations_for?: true)
ability.should be_able_to(:invite_to, network)
end
it "denies a member if the member's invitation privileges are disabled" do
@user = stub(admin?: false, can_send_invitations_for?: false)
ability.should_not be_able_to(:invite_to, network)
end
end
end
end
Failures
1) Ability#invite_to, Network when network level invitations are enabled allows an admin
Failure/Error: ability.should be_able_to(:invite_to, network)
expected to be able to :invite_to #<RSpec::Mocks::Mock:0x3fe3ed90444c @name=nil>
# ./spec/models/ability_spec.rb:16:in `block (4 levels) in <top (required)>'
2) Ability#invite_to, Network when network level invitations are enabled allows a member if the member's invitation privileges are enabled
Failure/Error: ability.should be_able_to(:invite_to, network)
expected to be able to :invite_to #<RSpec::Mocks::Mock:0x3fe3edc27408 @name=nil>
# ./spec/models/ability_spec.rb:21:in `block (4 levels) in <top (required)>'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您按照编写的方式运行代码,则永远不会到达 Can 块内的代码。对存根的调用返回 RSpec::Mocks::Mock 类的对象;它必须属于 Network 类,CanCan 才能应用该规则。
If you run the code as you wrote it, the code inside the Can block is never reached. Your call to stub returns an object of class RSpec::Mocks::Mock; it must be of class Network for CanCan to apply the rule.