This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #7: when generating ContextRoles, also generate unit tests
- Loading branch information
Rene van den Berg
committed
Feb 25, 2014
1 parent
e269b79
commit fa7f617
Showing
8 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
lib/generators/mini_test/context_role/context_role_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
lib/generators/mini_test/context_role/templates/role_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
lib/generators/rspec/context_role/context_role_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
lib/generators/test_unit/context_role/context_role_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
61
lib/generators/test_unit/context_role/templates/role_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |