简体   繁体   English

使用新的Rspec语法在CanCan中测试功能

[英]Testing abilities in CanCan with new Rspec syntax

I'm learning RSpec and I'm having a little trouble understanding shared subjects, and how it works with the new expect syntax. 我正在学习RSpec,在理解共享主题以及它如何与新的Expect语法一起工作时遇到了一些麻烦。

I followed a tutorial from the cancan wiki but i can't figure out how to test when a user should not be able to perform an action with the same syntax. 我遵循了cancan Wiki上教程,但是我无法弄清楚如何测试用户何时不应该使用相同的语法执行操作。

user_spec.rb: user_spec.rb:

require 'spec_helper'
require "cancan/matchers"

describe User do

  describe 'abilities' do
    let(:user) { FactoryGirl.create(:user) }
    let(:ability) { FactoryGirl.create(:user) }

    subject(:ability) { Ability.new(user) }

    it { should be_able_to :read, User.new }

    # clunky expectation 
    it 'should not be able to destroy others' do
      expect(ability).not_to  be_able_to(:destroy, User.new)
    end
  end
end

What i want to do is write an expectation like 我想做的是写一个期望像

it { should not be_able_to :delete, User.new }

Am I going about this wrong? 我要解决这个错误吗?

You can use the should_not syntax in your shortened version: 您可以在缩短的版本中使用should_not语法:

it { should_not be_able_to :delete, User.new }

or alternatively, with RSpec 3's synonyms: 或者,使用RSpec 3的同义词:

it { is_expected.not_to be_able_to :delete, User.new }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM