Module: Nutrasuite::ContextHelpers

Included in:
MiniTest::Unit::TestCase
Defined in:
lib/nutrasuite/contexts.rb

Overview

Public: ContextHelpers contains all of the Nutrasuite Context methods. This will be included in Test::Unit::TestCase by default.

Instance Method Summary collapse

Instance Method Details

#after(&block) ⇒ Object

Public: use after to declare steps that need to be run after every test in a context.



44
45
46
# File 'lib/nutrasuite/contexts.rb', line 44

def after(&block)
  Context.current_context.teardowns << block
end

#before(&block) ⇒ Object

Public: use before to declare steps that need to be run before every test in a context.



33
34
35
36
37
38
39
40
# File 'lib/nutrasuite/contexts.rb', line 33

def before(&block)
  if Context.current_context
    Context.current_context.setups << block
  else
    puts self.name + " is adding a hook"
    self.add_setup_hook(nil, &block)
  end
end

#build_test(name, options = {}, &block) ⇒ Object

Internal: This method actually builds out the test method that MiniTest knows how to execute. It’s responsible for running the current list of setups and teardowns and relies on ‘define_method’ to set up a test method that MiniTest can parse and execute.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nutrasuite/contexts.rb', line 92

def build_test(name, options = {}, &block)
  test_name = Context.build_test_name(name)

  setups = []
  # Be sure to include setup hooks from minitest.
  unless @setup_hooks.nil?
    setups = setups + @setup_hooks
  end

  teardowns = []
  Context.context_stack.each do |context|
    setups.concat(context.setups)
    teardowns.concat(context.teardowns)
  end

  if options[:skip]
    define_method test_name do
      skip "not yet implemented"
    end
  else
    define_method test_name do
      test = MiniTest::Unit::TestCase.current
      setups.each { |setup| test.instance_eval &setup }
      test.instance_eval &block
      teardowns.each { |teardown| test.instance_eval &teardown }
    end
  end
end

#it(name, &block) ⇒ Object

Public: defines a test to be executed. Will run any setup blocks on the context stack before execution, then execute the specified test, then will run any teardown blocks on the context stack after the test has executed.

name - The name of the test, used for human-readable test identification. &block - the body of the test, can use any and all assertions that would

otherwise be available to a MiniTest test.

Examples

it "tests that true is truthy" do
  assert true
end


62
63
64
# File 'lib/nutrasuite/contexts.rb', line 62

def it(name, &block)
  build_test(name, &block)
end

#it_eventually(name, &block) ⇒ Object

Public: defines a test method that should be skipped. Context setup/teardown will still be executed, but the actual test method will show up as a MiniTest skip with a description of “not yet implemented.”

This method exists to make it easy to switch a test between a pending and an active state: just switch the method name from “it” to “it eventually.”

name - The name of the test, used for human-readable test identification. &block - the body of the test. Doesn’t have to be functional ruby as the

block will be skipped in this method.

Examples

it_eventually "has some really smart logic" do
  assert self.has_smart_logic?
end


83
84
85
# File 'lib/nutrasuite/contexts.rb', line 83

def it_eventually(name, &block)
  build_test("eventually #{name}", :skip => true, &block)
end