简体   繁体   中英

Testing cancan abilities with Factory Girl

I'm new to RSpec and Factory Girl so I'm sure I'm missing something simple.

I would like to figure out how to incorporate Factory Girl into the approach to testing cancan abilities which is recommend on the cancan wiki :

require "cancan/matchers"
# create user
ability = Ability.new(user)
ability.should_not be_able_to(:destroy, Project.new)

I put this code in spec/models/ability_spec.rb and tried creating the user with the same Factory Girl method that is working in my controller specs:

  admin = create(:user)

However RSpec gives the error "undefined method `create'" The same thing happens when I do FactoryGirl.create(:user).

Update

In specs/spec_helper.rb I have:

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end

Any idea what's going wrong?

尝试将其放在您的spec_helper.rb中

config.include FactoryGirl::Syntax::Methods

Ok. Very embarrassing. I missed something simple - not putting this inside a proper it call.

Only consolation is that I'm not the first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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