Module: Test::Spec::TestCase::ClassMethods

Defined in:
lib/test/spec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#after_allObject

Returns the value of attribute after_all.



386
387
388
# File 'lib/test/spec.rb', line 386

def after_all
  @after_all
end

#before_allObject

Returns the value of attribute before_all.



385
386
387
# File 'lib/test/spec.rb', line 385

def before_all
  @before_all
end

#countObject

Returns the value of attribute count.



377
378
379
# File 'lib/test/spec.rb', line 377

def count
  @count
end

#nameObject

Returns the value of attribute name.



378
379
380
# File 'lib/test/spec.rb', line 378

def name
  @name
end

#parentObject

Returns the value of attribute parent.



380
381
382
# File 'lib/test/spec.rb', line 380

def parent
  @parent
end

#positionObject

Returns the value of attribute position.



379
380
381
# File 'lib/test/spec.rb', line 379

def position
  @position
end

#setupsObject

Returns the value of attribute setups.



382
383
384
# File 'lib/test/spec.rb', line 382

def setups
  @setups
end

#teardownsObject

Returns the value of attribute teardowns.



383
384
385
# File 'lib/test/spec.rb', line 383

def teardowns
  @teardowns
end

Instance Method Details

#after(kind = :each, &block) ⇒ Object



496
497
498
499
500
501
502
503
504
505
# File 'lib/test/spec.rb', line 496

def after(kind=:each, &block)
  case kind
  when :each
    teardown(&block)
  when :all
    after_all << block
  else
    raise ArgumentError, "invalid argument: after(#{kind.inspect})"
  end
end

#before(kind = :each, &block) ⇒ Object



485
486
487
488
489
490
491
492
493
494
# File 'lib/test/spec.rb', line 485

def before(kind=:each, &block)
  case kind
  when :each
    setup(&block)
  when :all
    before_all << block
  else
    raise ArgumentError, "invalid argument: before(#{kind.inspect})"
  end
end

#behaves_like(shared_context) ⇒ Object Also known as: it_should_behave_like



459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/test/spec.rb', line 459

def behaves_like(shared_context)
  if Test::Spec::SHARED_CONTEXTS.include?(shared_context)
    Test::Spec::SHARED_CONTEXTS[shared_context].each { |block|
      instance_eval(&block)
    }
  elsif Test::Spec::SHARED_CONTEXTS.include?(self.name + "\t" + shared_context)
    Test::Spec::SHARED_CONTEXTS[self.name + "\t" + shared_context].each { |block|
      instance_eval(&block)
    }
  else
    raise NameError, "Shared context #{shared_context} not found."
  end
end

#context(name_or_class, superclass = Test::Unit::TestCase, klass = Test::Spec::TestCase, &block) ⇒ Object Also known as: describe

old-style (RSpec <1.0):



390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/test/spec.rb', line 390

def context(name_or_class, superclass=Test::Unit::TestCase, klass=Test::Spec::TestCase, &block)
  cls_string = name_or_class.is_a?(String) ? name_or_class : name_or_class.class.to_s
  superclass = Test::Spec::RailsHelper.figure_out_superclass_from_name(name_or_class, superclass)
  
  superclass = self.superclass

  if self.respond_to?(:controller_class=)
    controller_klass = name.is_a?(Class) ? name : Test::Spec::RailsHelper::infer_controller_class(name)
    self.controller_class = controller_klass if controller_klass
  end

  (Test::Spec::CONTEXTS[self.name.to_s + "\t" + cls_string] ||= klass.new(cls_string, self, superclass)).add(&block)
end

#fcontext(name, superclass = Test::Unit::TestCase, klass = Test::Spec::TestCase, &block) ⇒ Object Also known as: fdescribe



404
405
406
407
# File 'lib/test/spec.rb', line 404

def fcontext(name, superclass=Test::Unit::TestCase, klass=Test::Spec::TestCase, &block)
  Test::Spec.set_focused_mode(true)
  context(name, superclass, Test::Spec::FocusedTestCase, &block)
end

#fspecify(specname, &block) ⇒ Object Also known as: fit



428
429
430
431
432
433
# File 'lib/test/spec.rb', line 428

def fspecify(specname, &block)
  Test::Spec.set_focused_mode(true, self.name)
  undef_previous_specs
  self.count += 1                 # Let them run in order of definition
  define_method("test_spec {%s} %03d [%s]" % [name, count, specname], &block)
end

#init(name, position, parent) ⇒ Object



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/test/spec.rb', line 508

def init(name, position, parent)
  self.position = position
  self.parent = parent
  
  if parent
    self.name = parent.name.to_s + "\t" + name
  else
    self.name = name.to_s
  end

  self.count = 0
  self.setups = []
  self.teardowns = []

  self.before_all = []
  self.after_all = []
end

#pspecify(specname, &block) ⇒ Object Also known as: pit



441
442
443
444
445
# File 'lib/test/spec.rb', line 441

def pspecify(specname, &block)
  specify specname do
    @_result.add_pending(specname)
  end
end

#setup(&block) ⇒ Object



447
448
449
# File 'lib/test/spec.rb', line 447

def setup(&block)
  setups << block
end

#shared_context(name, &block) ⇒ Object Also known as: describe_shared



455
456
457
# File 'lib/test/spec.rb', line 455

def shared_context(name, &block)
  Test::Spec::SHARED_CONTEXTS[self.name + "\t" + name] << block
end

#specify(specname, &block) ⇒ Object Also known as: it, they



413
414
415
416
417
418
419
420
# File 'lib/test/spec.rb', line 413

def specify(specname, &block)
  if block.nil?
     pspecify(specname)
  else
    self.count += 1                 # Let them run in order of definition
    define_method("test_spec {%s} %03d [%s]" % [name, count, specname], &block) unless Test::Spec.focused_mode?
  end
end

#teardown(&block) ⇒ Object



451
452
453
# File 'lib/test/spec.rb', line 451

def teardown(&block)
  teardowns << block
end

#undef_previous_specsObject



422
423
424
425
426
# File 'lib/test/spec.rb', line 422

def undef_previous_specs
  instance_methods.grep(/test_spec/).each do |meth|
    undef_method meth
  end
end

#xcontext(name, superclass = Test::Unit::TestCase, &block) ⇒ Object



409
410
411
# File 'lib/test/spec.rb', line 409

def xcontext(name, superclass=Test::Unit::TestCase, &block)
  context(name, superclass, Test::Spec::DisabledTestCase, &block)
end

#xspecify(specname, &block) ⇒ Object Also known as: xit



435
436
437
438
439
# File 'lib/test/spec.rb', line 435

def xspecify(specname, &block)
  specify specname do
    @_result.add_disabled(specname)
  end
end