用 rspec 测试康康康的能力
Testing cancancan abilities with rspec
我正在尝试使用 rspec
测试我的康康康舞能力
但与测试特定用户可以做什么相反,我正在尝试测试用户不应该做什么。
现在,我有一个这样的上下文块:
context "for a manager" do
before do
@manager = FactoryGirl.build(:user, :manager)
@ability = Ability.new(@manager)
end
it "should not be able to create Questions" do
expect(@ability).not_to be_able_to(:create, Question.new)
end
it "should not be able to read Questions" do
expect(@ability).not_to be_able_to(:read, Question.new)
end
it "should not be able to update Questions" do
expect(@ability).not_to be_able_to(:update, Question.new)
end
it "should not be able to delete Questions" do
expect(@ability).not_to be_able_to(:destroy, Question.new)
end
end
这清楚地表明 manager
类型的用户不应以任何形式访问 Question
模型。
有没有直接的方法将整个块写在一个 it
块中,只有一个 expect
?
我想过这样写:
context "for a manager" do
before do
@manager = FactoryGirl.build(:user, :manager)
@ability = Ability.new(@manager)
end
it "should not be able to manage Questions" do
expect(@ability).not_to be_able_to(:manage, Question.new)
end
end
但我认为这可能不一定会达到我的预期目的,因为此测试将通过与未授予该资源的一项能力一样多。
那么,简而言之,有没有直接的方法来测试这样的场景?感谢大家。
首先,我建议您使用下面示例中的 explicit subject
for @ability
so you can use the one-liner syntax。
describe Role do
subject(:ability){ Ability.new(user) }
let(:user){ FactoryGirl.build(:user, roles: [role]) }
context "when is a manager" do
let(:role){ FactoryGirl.build(:manager_role) }
it{ is_expected.not_to be_able_to(:create, Question.new) }
it{ is_expected.not_to be_able_to(:read, Question.new) }
it{ is_expected.not_to be_able_to(:update, Question.new) }
it{ is_expected.not_to be_able_to(:destroy, Question.new) }
end
end
在您发表评论后更新
但您也可以将所有这 4 个期望总结为简单
%i[create read update destroy].each do |role|
it{ is_expected.not_to be_able_to(role, Question.new) }
end
我正在尝试使用 rspec
测试我的康康康舞能力但与测试特定用户可以做什么相反,我正在尝试测试用户不应该做什么。
现在,我有一个这样的上下文块:
context "for a manager" do
before do
@manager = FactoryGirl.build(:user, :manager)
@ability = Ability.new(@manager)
end
it "should not be able to create Questions" do
expect(@ability).not_to be_able_to(:create, Question.new)
end
it "should not be able to read Questions" do
expect(@ability).not_to be_able_to(:read, Question.new)
end
it "should not be able to update Questions" do
expect(@ability).not_to be_able_to(:update, Question.new)
end
it "should not be able to delete Questions" do
expect(@ability).not_to be_able_to(:destroy, Question.new)
end
end
这清楚地表明 manager
类型的用户不应以任何形式访问 Question
模型。
有没有直接的方法将整个块写在一个 it
块中,只有一个 expect
?
我想过这样写:
context "for a manager" do
before do
@manager = FactoryGirl.build(:user, :manager)
@ability = Ability.new(@manager)
end
it "should not be able to manage Questions" do
expect(@ability).not_to be_able_to(:manage, Question.new)
end
end
但我认为这可能不一定会达到我的预期目的,因为此测试将通过与未授予该资源的一项能力一样多。
那么,简而言之,有没有直接的方法来测试这样的场景?感谢大家。
首先,我建议您使用下面示例中的 explicit subject
for @ability
so you can use the one-liner syntax。
describe Role do
subject(:ability){ Ability.new(user) }
let(:user){ FactoryGirl.build(:user, roles: [role]) }
context "when is a manager" do
let(:role){ FactoryGirl.build(:manager_role) }
it{ is_expected.not_to be_able_to(:create, Question.new) }
it{ is_expected.not_to be_able_to(:read, Question.new) }
it{ is_expected.not_to be_able_to(:update, Question.new) }
it{ is_expected.not_to be_able_to(:destroy, Question.new) }
end
end
在您发表评论后更新
但您也可以将所有这 4 个期望总结为简单
%i[create read update destroy].each do |role|
it{ is_expected.not_to be_able_to(role, Question.new) }
end