Module: Test::Unit::Given::Simple

Included in:
TestCase
Defined in:
lib/test/unit/given/simple.rb

Overview

A simple form that doesn’t enforce the use of Given/When/Then. This is merely to make it clear which parts of your test do what

Instance Method Summary collapse

Instance Method Details

#And(existing_block = nil, &block) ⇒ Object

Public: Continue the previous Given/When/Then in a new block. The reason you might do this is if you wanted to use the existing block form of a Given/When/Then, but also need to do something custom. This allows you to write your test more fluently.

existing_block - a callable object (e.g. a Proc) that will be called immediately

by this Then.  If nil, &block is expected to be passed

block - a block given to this call that will be executed immediately

by this Then.  If existing_block is non-nil, this is ignored

Examples

then_block = Then {
  assert_equal "bar",@foo
}
# => executes block and returns it
Then then_block
And {
  assert_equal, "quux",@blah
}
# => executes the block again

Returns the block that was executed



93
94
95
# File 'lib/test/unit/given/simple.rb', line 93

def And(existing_block=nil,&block)
  Given(existing_block,&block)
end

#But(existing_block = nil, &block) ⇒ Object

Public: Continue the previous Given/When/Then in a new block. The reason you might do this is if you wanted to use the existing block form of a Given/When/Then, but also need to do something custom, and that custom thing contradicts or overrides the flow, so an “And” wouldn’t read properly. This allows you to write your test more fluently.

existing_block - a callable object (e.g. a Proc) that will be called immediately

by this Then.  If nil, &block is expected to be passed

block - a block given to this call that will be executed immediately

by this Then.  If existing_block is non-nil, this is ignored

Examples

given_block = Given {
  @options = {
    :foo => 'bar',
    :quux => 'baz',
  }
}
Given then_block
And {
  @name = 'Foo'
}
But {
  @options[:quux] = 'BAZ'
}

Returns the block that was executed



125
126
127
# File 'lib/test/unit/given/simple.rb', line 125

def But(existing_block=nil,&block)
  Given(existing_block,&block)
end

#Given(existing_block = nil, &block) ⇒ Object

Public: Set up the conditions for the test

existing_block - a callable object (e.g. a Proc) that will be called immediately

by this Given.  If nil, &block is expected to be passed

block - a block given to this call that will be executed immediately

by this Given.  If existing_block is non-nil, this is ignored

Examples

given_block = Given {
  @foo = "bar"
}
# => executes block and returns it
Given given_block
# => executes the block again

Returns the block that was executed



25
26
27
# File 'lib/test/unit/given/simple.rb', line 25

def Given(existing_block=nil,&block)
  call_block_param_or_block_given(existing_block,&block)
end

#mocks_shouldve_been_calledObject

Similar to #test_runs, this is used to make clear what you are testing and what the assertions are. Since many Ruby mock frameworks do not require an explicit “verify” step, you often have tests that have no explicit asserts, the assertions being simply that the mocks were called as expected. This step, which is a no-op, allows you to document that you are expecting mocks to be called. See the example in #mocks_are_called for usage.



163
164
165
# File 'lib/test/unit/given/simple.rb', line 163

def mocks_shouldve_been_called
  lambda {}
end

#the_test_runsObject

Public: Used to make clear the structure of tests using mocks. This returns a no-op, and is intended to be used with a “When” to delineate the creation of a mock in a Given, and the expectations of a mock in a “Then”

Example:

Given {
  @google = mock()
}
When the_test_runs
Then {
  @google.expects(:search).with('foo').returns('bar')
}
Given {
  @my_search = Search.new(@google)
}
When {
  @result = @my_search.find('foo')
}
Then {
  assert_equal 'Found bar',@result
}
And mocks_shouldve_been_called


153
154
155
# File 'lib/test/unit/given/simple.rb', line 153

def the_test_runs
  lambda {}
end

#Then(existing_block = nil, &block) ⇒ Object

Public: Verify the results of the test

existing_block - a callable object (e.g. a Proc) that will be called immediately

by this Then.  If nil, &block is expected to be passed

block - a block given to this call that will be executed immediately

by this Then.  If existing_block is non-nil, this is ignored

Examples

then_block = Then {
  assert_equal "bar",@foo
}
# => executes block and returns it
Then then_block
# => executes the block again

Returns the block that was executed



66
67
68
# File 'lib/test/unit/given/simple.rb', line 66

def Then(existing_block=nil,&block)
  Given(existing_block,&block)
end

#When(existing_block = nil, &block) ⇒ Object

Public: Execute the code under test

existing_block - a callable object (e.g. a Proc) that will be called immediately

by this When.  If nil, &block is expected to be passed

block - a block given to this call that will be executed immediately

by this When.  If existing_block is non-nil, this is ignored

Examples

when_block = When {
  @foo.go
}
# => executes block and returns it
When when_block
# => executes the block again

Returns the block that was executed



45
46
47
# File 'lib/test/unit/given/simple.rb', line 45

def When(existing_block=nil,&block)
  Given(existing_block,&block)
end