include tests with cancan:ability generator - closes #350
This commit is contained in:
parent
6ef2c44f57
commit
6c1d685f2c
|
@ -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.
|
||||||
|
|
|
@ -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
|
||||||
|
|
16
lib/generators/cancan/ability/templates/ability_spec.rb
Normal file
16
lib/generators/cancan/ability/templates/ability_spec.rb
Normal 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
|
10
lib/generators/cancan/ability/templates/ability_test.rb
Normal file
10
lib/generators/cancan/ability/templates/ability_test.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user