Class: Test::Unit::TestCase

Inherits:
Object show all
Extended by:
Spec::Example::ExampleGroupMethods
Includes:
FlexMock::ArgumentTypes, FlexMock::MockContainer, Spec::Example::ExampleMethods
Defined in:
lib/mack/testing/test_case.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/test_unit.rb,
lib/gems/rspec-1.1.11/lib/spec/interop/test/unit/testcase.rb

Overview

This extension of the standard Test::Unit::TestCase makes RSpec available from within, so that you can do things like:

require ‘test/unit’ require ‘spec’

class MyTest < Test::Unit::TestCase

it "should work with Test::Unit assertions" do
  assert_equal 4, 2+1
end

def test_should_work_with_rspec_expectations
  (3+1).should == 5
end

end

See also Spec::Example::ExampleGroup

Instance Attribute Summary

Attributes included from Spec::Example::ExampleGroupMethods

#description_options, #description_text, #spec_path

Attributes included from Spec::Matchers::ModuleMethods

#last_matcher, #last_should

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Spec::Example::ExampleGroupMethods

create_nested_example_group, create_shared_example_group, describe, described_type, description, description_args, description_parts, description_text, example, examples, inherited, it_should_behave_like, number_of_examples, predicate_matchers, register, registration_backtrace, reset, run_after_each, run_before_each, set_description, unregister, xexample

Methods included from Spec::Example::BeforeAndAfterHooks

#after_all_parts, #after_each_parts, #append_after, #append_before, #before_all_parts, #before_each_parts, #prepend_after, #prepend_before, #remove_after, #setup

Methods included from Spec::Example::ExampleMethods

#__full_description, #description, #eval_block, #eval_each_fail_fast, #eval_each_fail_slow, #execute, #implementation_backtrace, #instance_variable_hash, #options, #set_instance_variables_from_hash, #violated

Methods included from Spec::Example::ModuleReopeningFix

#child_modules, #include, #included

Methods included from Spec::Example::ModuleThatIsReopened

#module_that_is_reopened_method

Methods included from Spec::Example::Pending

#pending

Methods included from Spec::Matchers

#be, #be_close, #change, #eql, #equal, #exception_from, #exist, #fail, #fail_with, #have, #have_at_least, #have_at_most, #include, #map_specs, #match, #method_missing, #raise_error, #respond_to, #run_with, #satisfy, #simple_matcher, #smart_match, #throw_symbol

Methods included from Spec::Matchers::ModuleMethods

#clear_generated_description, #generated_description

Methods included from Mack::Testing::Helpers

#assigns, #clear_session, #cookies, #delete, #file_for_upload, #get, #in_session, #post, #put, #remote_test, #remove_cookie, #request, #response, #responses, #session, #set_cookie, #temp_app_config

Methods included from Mack::Routes::Urls

create_method, #redirect_html, #url_for_pattern

Methods included from FlexMock::MockContainer

#flexmock, #flexmock_close, #flexmock_remember, #flexmock_teardown, #flexmock_verify, #rails_version, #should_render_view

Methods included from FlexMock::Ordering

#flexmock_allocate_order, #flexmock_current_order, #flexmock_current_order=, #flexmock_groups, #flexmock_validate_order

Methods included from FlexMock::ArgumentTypes

#any, #eq, #on

Constructor Details

#initialize(defined_description, options = {}, &implementation) ⇒ TestCase

Returns a new instance of TestCase.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gems/rspec-1.1.11/lib/spec/interop/test/unit/testcase.rb', line 46

def initialize(defined_description, options={}, &implementation)
  @_defined_description = defined_description
  
  # TODO - examples fail in rspec-rails if we remove "|| pending_implementation"
  #      - find a way to fail without it in rspec's code examples
  @_implementation = implementation || pending_implementation

  @_result = ::Test::Unit::TestResult.new
  # @method_name is important to set here because it "complies" with Test::Unit's interface.
  # Some Test::Unit extensions depend on @method_name being present.
  @method_name = @_defined_description
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Spec::Matchers

Class Method Details

.example_method?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/gems/rspec-1.1.11/lib/spec/interop/test/unit/testcase.rb', line 34

def example_method?(method_name)
  should_method?(method_name) || test_method?(method_name)
end

.suiteObject



30
31
32
# File 'lib/gems/rspec-1.1.11/lib/spec/interop/test/unit/testcase.rb', line 30

def suite
  Test::Unit::TestSuiteAdapter.new(self)
end

.test_method?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/gems/rspec-1.1.11/lib/spec/interop/test/unit/testcase.rb', line 38

def test_method?(method_name)
  method_name =~ /^test[_A-Z]./ && (
    instance_method(method_name).arity == 0 ||
    instance_method(method_name).arity == -1
  )
end

Instance Method Details

#flexmock_original_teardownObject

Alias the original teardown behavior for later use.



21
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/test_unit.rb', line 21

alias :flexmock_original_teardown :teardown

#log_end(name = "") ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/mack/testing/test_case.rb', line 34

def log_end(name = "")                 
  @log_end_time = Time.now
  puts "#{format_log_time(@log_end_time)}: Ending\t#{name}"
  et = @log_end_time - @log_start_time
  suffix = ""
  et.round.times { suffix += "!" } if et > 1.0
  if et > 0.5
    puts "#{suffix}Elapsed Time: #{et} seconds#{suffix}"
  end
end

#log_start(name = "") ⇒ Object



29
30
31
32
# File 'lib/mack/testing/test_case.rb', line 29

def log_start(name = "")
  @log_start_time = Time.now      
  puts "\n#{format_log_time(@log_start_time)}: Starting\t#{name}"
end

#nameObject

:nodoc:



5
6
7
# File 'lib/mack/testing/test_case.rb', line 5

def name # :nodoc:
  "#{self.class.name}\t\t#{@method_name}"
end

#run(ignore_this_argument = nil) ⇒ Object

We need to wrap the run method so we can do things like run a cleanup method if it exists



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mack/testing/test_case.rb', line 15

def run(result, &progress_block) # :nodoc:
  super_run(result) do |state, name|
    in_session do
      if state == Test::Unit::TestCase::STARTED
        cleanup if self.respond_to?(:cleanup)
        log_start(name)
      else
        cleanup if self.respond_to?(:cleanup)
        log_end(name)
      end
    end
  end
end

#teardownObject

Teardown the test case, verifying any mocks that might have been defined in this test case.



25
26
27
28
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/test_unit.rb', line 25

def teardown
  flexmock_teardown
  flexmock_original_teardown
end