Rspec:自定义匹配器:想要一个行为与现有匹配器正好相反的匹配器
Rspec : Custom Matchers : want a matcher with behavior just opposite of existing matcher
我有情况
let(:user) { create(:user, organization: org, role_ids: [Role::ROLE_CSA]) }
subject(:ability) { Ability.new(user) }
describe 'Not Permitted' do
# I need this
it { is_expected_not_to (be_able_to(:index, AdminsController)) }
# Or this
it { is_expected_to (not_be_able_to(:index, AdminsController)) }
end
但是不幸的是我发现is_expected_not_to
和not_be_able_to
不可用。
我可以这样做
it 'should not permit' do
expect(ability).not_to be_able_to(:index, AdminsController)
end
但如果可能的话,我想改用 short form
。有什么办法可以实现我想要的吗?
如果没有,我想创建新的 custom matcher
,其行为与 CanCan
或 is_expected_to
的 be_able_to
相反(如果可能的话)。有人可以帮忙吗?
来自:https://www.relishapp.com/rspec/rspec-core/docs/subject/one-liner-syntax
你应该可以做到:
it { is_expected.not_to (be_able_to(:index, AdminsController)) }
我有情况
let(:user) { create(:user, organization: org, role_ids: [Role::ROLE_CSA]) }
subject(:ability) { Ability.new(user) }
describe 'Not Permitted' do
# I need this
it { is_expected_not_to (be_able_to(:index, AdminsController)) }
# Or this
it { is_expected_to (not_be_able_to(:index, AdminsController)) }
end
但是不幸的是我发现is_expected_not_to
和not_be_able_to
不可用。
我可以这样做
it 'should not permit' do
expect(ability).not_to be_able_to(:index, AdminsController)
end
但如果可能的话,我想改用 short form
。有什么办法可以实现我想要的吗?
如果没有,我想创建新的 custom matcher
,其行为与 CanCan
或 is_expected_to
的 be_able_to
相反(如果可能的话)。有人可以帮忙吗?
来自:https://www.relishapp.com/rspec/rspec-core/docs/subject/one-liner-syntax
你应该可以做到:
it { is_expected.not_to (be_able_to(:index, AdminsController)) }