include tests with cancan:ability generator - closes #350

This commit is contained in:
Ryan Bates 2011-09-28 11:02:18 -07:00
parent 6ef2c44f57
commit 6c1d685f2c
4 changed files with 34 additions and 2 deletions

View File

@ -1,4 +1,5 @@
Description: Description:
The cancan:ability generator creates an Ability class in the models The cancan:ability generator creates an Ability class in the models
directory. You can move this file anywhere you want as long as it directory. You can move this file anywhere you want as long as it
is in the load path. is in the load path. A test/spec file is also generated depending
on if a spec directory exists.

View File

@ -1,10 +1,15 @@
module Cancan module Cancan
module Generators module Generators
class AbilityGenerator < Rails::Generators::Base class AbilityGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__) source_root File.expand_path("../templates", __FILE__)
def generate_ability def generate_ability
copy_file "ability.rb", "app/models/ability.rb" copy_file "ability.rb", "app/models/ability.rb"
if File.exist?(destination_path("spec"))
copy_file "ability_spec.rb", "spec/models/ability_spec.rb"
else
copy_file "ability_test.rb", "test/unit/ability_test.rb"
end
end end
end end
end end

View File

@ -0,0 +1,16 @@
require "spec_helper"
require "cancan/matchers"
describe Ability do
describe "as guest" do
before(:each) do
@ability = Ability.new(nil)
end
it "can only create a user" do
# Define what a guest can and cannot do
# @ability.should be_able_to(:create, :users)
# @ability.should_not be_able_to(:update, :users)
end
end
end

View File

@ -0,0 +1,10 @@
require "test_helper"
class AbilityTest < ActiveSupport::TestCase
def guest_can_only_create_user
ability = Ability.new(nil)
# Define what a guest can and cannot do
# assert ability.can?(:create, :users)
# assert ability.cannot?(:update, :users)
end
end