Differences between rake spec & rspec spec

Hi everyone,

For the most part I’ve never had any issues running tests in a
combination of rake spec and rspec spec.

I ran into a weird issue today, when using CanCan and view tests.
All the tests were passing under rspec spec, but rake spec failed
them.

I had a line like so:

  • if can? :activate, Learner
    • if learner.new_record?
      = f.input :active

This was returning false, in the relevant test, it shouldn’t have.

So I dug deeper and put a raise in CanCan to inspect what was going
on:

def can?(*args)
  current_ability.can?(*args)
  raise "#{args}\n#{current_ability.inspect}"
end

This gave me two different results depending on rspec spec and rake
spec.

rspec spec:

ActionView::Template::Error:
[:index, School(id: integer, number: integer, name: string,
unit_identity: string, sysid: string)]
#<Ability:0x007fcffcc69ef8 @rules=[#<CanCan::Rule:
0x007fcffcc69d68 @match_all=false, @base_behavior=true, @actions
trunc’d

rake spec:

ActionView::Template::Error:
[:index, School(id: integer, number: integer, name: string,
unit_identity: string, sysid: string)]
#<Ability:0x007fd02cf43f40 @rules=[]>

To get around this I stubbed CanCan for the view tests, and the tests
passed on both rake spec and rspec spec.
So everything is good and well.

But what strikes me is the absence of the @rules (coming from loading
CanCan and the ability.rb file) in the rake spec task.
Seems that rspec spec is doing the correct thing, but something isn’t
coming in when loaded on rake spec.

I am aware there are differences (GitHub - rspec/rspec-rails: RSpec for Rails 5+
issues/208#issuecomment-413114) between the two, but not sure how
that would affect this, or if just ignoring rake spec completely (what
about the db stuff rake spec does)?

Would enjoy hearing everyones thoughts…