Class: Test::Unit::TestCase

Inherits:
Object
  • Object
show all
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

Attributes included from Context::Context

#context_list

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Context::Context

context, context_name, context_name=

Class Attribute Details

.after_all_callbacksObject

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_callbacksObject

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_callbacksObject

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_callbacksObject

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

.before_should_callbacksObject

Returns the value of attribute before_should_callbacks.



3
4
5
# File 'lib/context/lifecycle.rb', line 3

def before_should_callbacks
  @before_should_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


28
29
30
31
32
33
34
35
# File 'lib/context/lifecycle.rb', line 28

def after(period = :each, &block)
  unless block_given?
    block  = period
    period = :each
  end

  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
14
15
16
17
18
# File 'lib/context/lifecycle.rb', line 11

def before(period = :each, &block)
  unless block_given?
    block  = period
    period = :each
  end

  send("before_#{period}_callbacks") << block
end

.before_test(name, &block) ⇒ Object



31
32
33
# File 'lib/context/test.rb', line 31

def before_test(name, &block)
  test(name, :before => block) {}
end

.gather_callbacks(callback_type, period) ⇒ Object

:nodoc:



39
40
41
42
# File 'lib/context/lifecycle.rb', line 39

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:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/context/lifecycle.rb', line 51

def self.inherited(child) # :nodoc:
  super
  child.before_all_callbacks    = []
  child.before_each_callbacks   = []
  child.after_each_callbacks    = []
  child.after_all_callbacks     = []
  child.before_should_callbacks = {}

  child.class_eval do
    def setup(&block)
      super
      
      unless self.class.before_should_callbacks[method_name].nil?
        instance_eval(&self.class.before_should_callbacks[method_name])
      end

      run_each_callbacks :before
    end

    def teardown
      super

      run_each_callbacks :after
    end
  end if self == Test::Unit::TestCase
end

.setup_for_shouldaObject



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

.suiteObject

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, opts = {}, &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
24
25
26
27
# File 'lib/context/test.rb', line 10

def test(name, opts={}, &block)
  test_name = ["test:", context_name, name].reject { |n| n == "" }.join(' ')
  # puts "running test #{test_name}"
  defined = instance_method(test_name) rescue false
  raise "#{test_name} is already defined in #{self}" if defined

  unless opts[:before].nil?
    before_should_callbacks[test_name] = opts[:before]
  end
  
  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:



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/context/lifecycle.rb', line 84

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
rescue Object => exception
  raise <<-BANG
Error running the #{callback_type}(:all) callback for #{name}
#{exception.class.name}: #{exception.message}
#{exception.backtrace.join("\n")}
  BANG
end

#run_each_callbacks(callback_type) ⇒ Object

:nodoc:



78
79
80
81
82
# File 'lib/context/lifecycle.rb', line 78

def run_each_callbacks(callback_type) # :nodoc:
  self.class.gather_callbacks(callback_type, :each).each do |c| 
    c.is_a?(Proc) ? instance_eval(&c) : send(c)
  end
end

#set_values_from_callbacks(values) ⇒ Object

:nodoc:



98
99
100
101
102
# File 'lib/context/lifecycle.rb', line 98

def set_values_from_callbacks(values) # :nodoc:
  values.each do |name, value|
    instance_variable_set name, value
  end
end