Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
Refs #7: when generating ContextRoles, also generate unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rene van den Berg committed Feb 25, 2014
1 parent e269b79 commit fa7f617
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/generators/arrthorizer/context_role/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ Example:

This will create:
app/roles/forum/post_author.rb

and a unit test file for the new role, depending
on your framework:
rspec: spec/roles/forum/post_author_spec.rb
test_unit: test/roles/forum/post_author_test.rb
mini_test: test/roles/forum/post_author_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ class Arrthorizer::ContextRoleGenerator < Rails::Generators::NamedBase
def create_role
template "role.rb", "app/roles/#{name}.rb"
end

hook_for :test_framework
end
11 changes: 11 additions & 0 deletions lib/generators/mini_test/context_role/context_role_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module MiniTest
module Generators
class ContextRoleGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)

def unit_test
template "role_test.rb", "test/roles/#{name}_test.rb"
end
end
end
end
62 changes: 62 additions & 0 deletions lib/generators/mini_test/context_role/templates/role_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'test_helper'

class <%= class_name %>Test < ActiveSupport::TestCase
def user
@user ||= OpenStruct.new
end
def context_hash
@context_hash ||= {}
end
def current_context
Arrthorizer::Context.new(context_hash)
end
def role
<%= class_name %>
end

def make_role_apply!
# TODO: make the changes to the context_hash that make the role
# apply to the user
end

def make_role_not_apply!
# TODO: make the changes to the context_hash that make the role
# *not* apply to the user
end

def test_returns_true_when_some_context
make_role_apply!

failure_message = "Expected #{role} to apply when context = #{current_context}"
assert role.applies_to_user?(user, current_context), failure_message
end

def test_returns_false_when_some_other_context
make_role_not_apply!

failure_message = "Expected #{role} not_to apply when context = #{current_context}"
refute role.applies_to_user?(user, current_context), failure_message
end

def test_when_true_no_state_is_maintained_in_instance
make_role_apply!

role.applies_to_user?(user, current_context)
ivars = role.instance.instance_variables

failure_message = "Expected apply_to_user? not to change state for #{role} (on instance), but it did"
assert_empty ivars, failure_message
end

def test_when_false_no_state_is_maintained_in_instance
make_role_not_apply!

failure_message = "Expected apply_to_user? not to change state for #{role} (on instance), but it did"
ivars = role.instance.instance_variables

assert_empty ivars, failure_message
end
end
11 changes: 11 additions & 0 deletions lib/generators/rspec/context_role/context_role_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Rspec
module Generators
class ContextRoleGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)

def unit_test
template "role_spec.rb", "spec/roles/#{name}_spec.rb"
end
end
end
end
53 changes: 53 additions & 0 deletions lib/generators/rspec/context_role/templates/role_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'spec_helper'

describe <%= class_name %> do
subject(:role) { <%= class_name %> }

let(:user) { double(:user) }

let(:context_hash) { { } }
let(:current_context) { Arrthorizer::Context.new(context_hash) }

describe :applies_to_user? do
context "when some_condition" do
before :each do
# TODO: Add the required elements to the context_hash to make the ContextRole apply to the user
end

it "returns true" do
pending

expect(role.applies_to_user?(user, current_context)).to be_true
end

# This is an extremely important test - it safeguards against
# persisting data between requests.
specify "no state is maintained in the role object" do
role.applies_to_user?(user, current_context)

role.instance.instance_variables.should be_empty
end
end

context "when some_other_condition" do
before :each do
# TODO: Add the required elements to the context_hash
# to make the ContextRole *not* apply to the user
end

it "returns false" do
pending

expect(role.applies_to_user?(user, current_context)).to be_false
end

# This is an extremely important test - it safeguards against
# persisting data between requests.
specify "no state is maintained in the role object" do
role.applies_to_user?(user, current_context)

role.instance.instance_variables.should be_empty
end
end
end
end
11 changes: 11 additions & 0 deletions lib/generators/test_unit/context_role/context_role_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module TestUnit
module Generators
class ContextRoleGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../templates', __FILE__)

def unit_test
template "role_test.rb", "test/roles/#{name}_test.rb"
end
end
end
end
61 changes: 61 additions & 0 deletions lib/generators/test_unit/context_role/templates/role_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'test_helper'

class <%= class_name %>Test < ActiveSupport::TestCase
def user
@user ||= OpenStruct.new
end
def context_hash
@context_hash ||= {}
end
def current_context
Arrthorizer::Context.new(context_hash)
end
def role
<%= class_name %>
end

def make_role_apply!
# TODO: make the changes to the context_hash that make the role
# apply to the user
end

def make_role_not_apply!
# TODO: make the changes to the context_hash that make the role
# *not* apply to the user
end

test "returns true when some context" do
make_role_apply!

failure_message = "Expected #{role} to apply when context = #{current_context}"
assert role.applies_to_user?(user, current_context), failure_message
end

test "returns false when some other context" do
make_role_not_apply!

failure_message = "Expected #{role} not_to apply when context = #{current_context}"
assert !role.applies_to_user?(user, current_context), failure_message
end

test "when true no state is maintained in role" do
make_role_apply!

role.applies_to_user?(user, current_context)

failure_message = "Expected apply_to_user? not to change state for #{role} (on instance), but it did"
assert_empty role.instance.instance_variables, failure_message
end

test "when false no state is maintained in role" do
make_role_not_apply!

role.applies_to_user?(user, current_context)

failure_message = "Expected apply_to_user? not to change state for #{role} (on instance), but it did"
assert_empty role.instance.instance_variables, failure_message
end
end

0 comments on commit fa7f617

Please sign in to comment.