Class: Test::Unit::TestCase
- Inherits:
-
Object
- Object
- Test::Unit::TestCase
- Extended by:
- Context::Context
- Defined in:
- lib/context/suite.rb,
lib/context/lifecycle.rb,
lib/context/shared_behavior.rb,
lib/context/core_ext/rails_hacks.rb,
lib/context/test.rb,
lib/context.rb
Class Attribute Summary collapse
-
.after_all_callbacks ⇒ Object
Returns the value of attribute after_all_callbacks.
-
.after_each_callbacks ⇒ Object
Returns the value of attribute after_each_callbacks.
-
.before_all_callbacks ⇒ Object
Returns the value of attribute before_all_callbacks.
-
.before_each_callbacks ⇒ Object
Returns the value of attribute before_each_callbacks.
Attributes included from Context::Context
Class Method Summary collapse
-
.after(period = :each, &block) ⇒ Object
(also: teardown)
Add logic to run after the tests (i.e., a
teardownmethod). -
.before(period = :each, &block) ⇒ Object
(also: setup)
Add logic to run before the tests (i.e., a
setupmethod). -
.gather_callbacks(callback_type, period) ⇒ Object
:nodoc:.
-
.inherited(child) ⇒ Object
:nodoc:.
- .setup_for_shoulda ⇒ Object
-
.shared(name, &block) ⇒ Object
Share behavior among different contexts.
-
.suite ⇒ Object
Tweaks to standard method so we don’t get superclass methods and we don’t get weird default tests.
-
.test(name, &block) ⇒ Object
Create a test method.
-
.use(shared_name) ⇒ Object
Pull in behavior shared by
sharedor a module.
Instance Method Summary collapse
-
#run_all_callbacks(callback_type) ⇒ Object
:nodoc:.
-
#run_each_callbacks(callback_type) ⇒ Object
:nodoc:.
-
#set_values_from_callbacks(values) ⇒ Object
:nodoc:.
Methods included from Context::Context
context, context_name, context_name=
Class Attribute Details
.after_all_callbacks ⇒ Object
Returns the value of attribute after_all_callbacks.
3 4 5 |
# File 'lib/context/lifecycle.rb', line 3 def after_all_callbacks @after_all_callbacks end |
.after_each_callbacks ⇒ Object
Returns the value of attribute after_each_callbacks.
3 4 5 |
# File 'lib/context/lifecycle.rb', line 3 def after_each_callbacks @after_each_callbacks end |
.before_all_callbacks ⇒ Object
Returns the value of attribute before_all_callbacks.
3 4 5 |
# File 'lib/context/lifecycle.rb', line 3 def before_all_callbacks @before_all_callbacks end |
.before_each_callbacks ⇒ Object
Returns the value of attribute before_each_callbacks.
3 4 5 |
# File 'lib/context/lifecycle.rb', line 3 def before_each_callbacks @before_each_callbacks end |
Class Method Details
.after(period = :each, &block) ⇒ Object Also known as: teardown
Add logic to run after the tests (i.e., a teardown method)
after do
User.delete_all
end
23 24 25 |
# File 'lib/context/lifecycle.rb', line 23 def after(period = :each, &block) send("after_#{period}_callbacks") << block end |
.before(period = :each, &block) ⇒ Object Also known as: setup
Add logic to run before the tests (i.e., a setup method)
before do
@user = User.first
end
11 12 13 |
# File 'lib/context/lifecycle.rb', line 11 def before(period = :each, &block) send("before_#{period}_callbacks") << block end |
.gather_callbacks(callback_type, period) ⇒ Object
:nodoc:
29 30 31 32 |
# File 'lib/context/lifecycle.rb', line 29 def gather_callbacks(callback_type, period) # :nodoc: callbacks = superclass.respond_to?(:gather_callbacks) ? superclass.gather_callbacks(callback_type, period) : [] callbacks.push(*send("#{callback_type}_#{period}_callbacks")) end |
.inherited(child) ⇒ Object
:nodoc:
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/context/lifecycle.rb', line 40 def self.inherited(child) # :nodoc: super child.before_all_callbacks = [] child.before_each_callbacks = [] child.after_each_callbacks = [] child.after_all_callbacks = [] child.class_eval do def setup(&block) super run_each_callbacks :before end def teardown super run_each_callbacks :after end end end |
.setup_for_shoulda ⇒ Object
3 4 5 6 7 8 |
# File 'lib/context/core_ext/rails_hacks.rb', line 3 def setup_for_shoulda self.instance_eval do alias :setup :before alias :teardown :after end end |
.shared(name, &block) ⇒ Object
Share behavior among different contexts. This creates a module (actually, a Module subclass) that is included using the use method (or one of its aliases) provided by context or include if you know the module’s constant name.
Examples
shared "other things" do
it "should do things but not some things" do
# behavior is fun
end
end
use "other things"
# or...
it_should_behave_like "other things"
shared :client do
it "should be a client to our server" do
# TODO: client behavior here
end
end
use :client
# or...
uses "client"
behaves_like "client"
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/context/shared_behavior.rb', line 49 def shared(name, &block) case name.class.name when "String" name = name.to_module_name when "Symbol" name = name.to_s.to_module_name else raise ArgumentError, "Provide a String or Symbol as the name of the shared behavior group" end Object.const_set(name, Context::SharedBehavior.create_from_behavior(block)) end |
.suite ⇒ Object
Tweaks to standard method so we don’t get superclass methods and we don’t get weird default tests
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/context/suite.rb', line 7 def suite # :nodoc: method_names = public_instance_methods - superclass.public_instance_methods tests = method_names.delete_if {|method_name| method_name !~ /^test./} suite = Test::Unit::TestSuite.new(name) tests.sort.each do |test| catch(:invalid_test) do suite << new(test) end end suite end |
.test(name, &block) ⇒ Object
Create a test method. name is a native-language string to describe the test (e.g., no more test_this_crazy_thing_with_underscores).
test "A user should not be able to delete another user" do
assert_false @user.can?(:delete, @other_user)
end
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/context/test.rb', line 10 def test(name, &block) test_name = "test_#{((context_name == "" ? context_name : context_name + " ") + name).to_method_name}".to_sym # puts "running test #{test_name}" defined = instance_method(test_name) rescue false raise "#{test_name} is already defined in #{self}" if defined if block_given? define_method(test_name, &block) else define_method(test_name) do flunk "No implementation provided for #{name}" end end end |
.use(shared_name) ⇒ Object
Pull in behavior shared by shared or a module.
Examples
shared "other things" do
it "should do things but not some things" do
# behavior is fun
end
end
use "other things"
# or...
it_should_behave_like "other things"
module Things
end
uses Things
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/context/shared_behavior.rb', line 83 def use(shared_name) case shared_name.class.name when "Context::SharedBehavior", "Module" include shared_name when "String" include Object.const_get(shared_name.to_module_name) when "Symbol" include Object.const_get(shared_name.to_s.to_module_name) else raise ArgumentError, "Provide a String or Symbol as the name of the shared behavior group or the module name" end end |
Instance Method Details
#run_all_callbacks(callback_type) ⇒ Object
:nodoc:
66 67 68 69 70 71 72 |
# File 'lib/context/lifecycle.rb', line 66 def run_all_callbacks(callback_type) # :nodoc: previous_ivars = instance_variables self.class.gather_callbacks(callback_type, :all).each { |c| instance_eval(&c) if c } (instance_variables - previous_ivars).inject({}) do |hash, ivar| hash.update ivar => instance_variable_get(ivar) end end |
#run_each_callbacks(callback_type) ⇒ Object
:nodoc:
62 63 64 |
# File 'lib/context/lifecycle.rb', line 62 def run_each_callbacks(callback_type) # :nodoc: self.class.gather_callbacks(callback_type, :each).each { |c| instance_eval(&c) if c } end |
#set_values_from_callbacks(values) ⇒ Object
:nodoc:
74 75 76 77 78 |
# File 'lib/context/lifecycle.rb', line 74 def set_values_from_callbacks(values) # :nodoc: values.each do |name, value| instance_variable_set name, value end end |