rspec_rails_scaffold_templates

RSpec scaffold generator templates that

Installation

Add to your Gemfile

gem 'rspec_rails_scaffold_templates'

Then bundle as usual.

Usage

Next time you run

rails generate scaffold SomeModel ...

you will get the needed specs for controller and views.

Configuring

In order to have in the generated specs the code that works around the code that checks ability you have to add to the application.rb file the config for generators as follows

config.generators do |g|
  g.cancan true
end

For the specs on views it will be

before(:each) do
  allow(controller).to receive(:can?).and_return(true)
end

while in the controller and request spec it will be

before :each do
  allow(controller).to receive(:current_user).and_return(current_user)
end

So, you need to add a method current_user to your rspec support, say, like this

def current_user(stubs = {})
  @mock_current_user ||= mock_model(User, name: 'Mock Current User').tap do |user|
    stubs.reverse_merge! is?: true, landing: nil
    stubs.each do |k, v|
      allow(user).to receive(k) {v}
    end
  end
end

You may want to stub current_ability instead of current_user in controller. For doing this replace true with :current_ability in g.cancan config like this

config.generators do |g|
  g.cancan :current_ability
end

Of course, you will need method current_ability in you spec support in this case, say, like this

class GodAbility
  include CanCan::Ability

  def initialize
    can :manage, :all
  end
end

def current_ability
  GodAbility.new
end

Contributing to rspec_rails_scaffold_templates

Copyright © 2014 Dmitri Koulikoff. See LICENSE.txt for further details.