class Protest::TestCase

A TestCase defines a suite of related tests. You can further categorize your tests by declaring nested contexts inside the class. See ::context.

Attributes

description[RW]

Fancy name for your test case, reports can use this to give nice, descriptive output when running your tests.

Public Class Methods

after(&block) click to toggle source
# File lib/protest/test_case.rb, line 99
def after(&block)
  warn "[DEPRECATED] `after` alias is deprecated. Use `teardown` instead."
  teardown(&block)
end
before(&block) click to toggle source
# File lib/protest/test_case.rb, line 94
def before(&block)
  warn "[DEPRECATED] `before` alias is deprecated. Use `setup` instead."
  setup(&block)
end
context(description, &block) click to toggle source

Define a new test context nested under the current one. All setup and teardown blocks defined on the current context will be inherited by the new context. This method is aliased as describe for your comfort.

# File lib/protest/test_case.rb, line 68
def self.context(description, &block)
  subclass = Class.new(self)
  subclass.class_eval(&block) if block
  subclass.description = description
  const_set(sanitize_description(description), subclass)
end
Also aliased as: describe
describe(description, &block)
Alias for: context
it(name, &block)
Alias for: test
new(name, location, &block) click to toggle source

Initialize a new instance of a single test. This test can be run in isolation by calling #run.

# File lib/protest/test_case.rb, line 107
def initialize(name, location, &block)
  @test = block
  @location = location
  @name = name
end
run(runner) click to toggle source

Run all tests in this context. Takes a Runner instance in order to provide output.

# File lib/protest/test_case.rb, line 34
def self.run(runner)
  tests.each {|test| runner.report(test) }
end
scenario(name, &block) click to toggle source
# File lib/protest/test_case.rb, line 89
def scenario(name, &block)
  warn "[DEPRECATED] `scenario` alias is deprecated. Use `test`, `it` or `should` instead."
  test(name, &block)
end
setup(&block) click to toggle source

Add a setup block to be run before each test in this context.

Calls superclass method
# File lib/protest/test_case.rb, line 50
def self.setup(&block)
  define_method :setup do
    super()
    instance_eval(&block)
  end
end
should(name, &block)
Alias for: test
story(description, &block) click to toggle source
# File lib/protest/test_case.rb, line 84
def story(description, &block)
  warn "[DEPRECATED] `story` alias is deprecated. Use `describe` or `context` instead."
  context(description, &block)
end
teardown(&block) click to toggle source

Add a teardown block to be run after each test in this context.

Calls superclass method
# File lib/protest/test_case.rb, line 58
def self.teardown(&block)
  define_method :teardown do
    instance_eval(&block)
    super()
  end
end
test(name, &block) click to toggle source

Add a test to be run in this context. This method is aliased as it and should for your comfort.

# File lib/protest/test_case.rb, line 45
def self.test(name, &block)
  tests << new(name, caller.at(0), &block)
end
Also aliased as: it, should
tests() click to toggle source

Tests added to this context.

# File lib/protest/test_case.rb, line 39
def self.tests
  @tests ||= []
end

Private Class Methods

inherited(child) click to toggle source
# File lib/protest/test_case.rb, line 193
def self.inherited(child)
  Protest.test_cases << child
end
sanitize_description(description) click to toggle source
# File lib/protest/test_case.rb, line 183
def self.sanitize_description(description)
  "Test#{description.gsub(/\W+/, ' ').strip.gsub(/(^| )(\w)/) { $2.upcase }}".to_sym
end

Public Instance Methods

assert(condition, message="Expected condition to be satisfied") click to toggle source

Ensure a condition is met. This will raise AssertionFailed if the condition isn't met. You can override the default failure message by passing it as an argument.

# File lib/protest/test_case.rb, line 136
def assert(condition, message="Expected condition to be satisfied")
  @report.on_assertion
  raise AssertionFailed, message unless condition
end
assert_equal(expected, actual, message=nil) click to toggle source

Passes if expected == actual. You can override the default failure message by passing it as an argument.

# File lib/protest/test_case.rb, line 143
def assert_equal(expected, actual, message=nil)
  assert expected == actual, message || "#{expected.inspect} expected but was #{actual.inspect}"
end
assert_raise(exception_class=Exception, message=nil) { || ... } click to toggle source

Passes if the code block raises the specified exception. If no exception is specified, passes if any exception is raised, otherwise it fails. You can override the default failure message by passing it as an argument.

# File lib/protest/test_case.rb, line 151
def assert_raise(exception_class=Exception, message=nil)
  begin
    yield
  rescue exception_class => e
  ensure
    assert e, message || "Expected #{exception_class.name} to be raised"
  end
end
name() click to toggle source

Name of the test

# File lib/protest/test_case.rb, line 167
def name
  @name
end
pending(message="Not Yet Implemented") click to toggle source

Make the test be ignored as pending. You can override the default message that will be sent to the report by passing it as an argument.

# File lib/protest/test_case.rb, line 162
def pending(message="Not Yet Implemented")
  raise Pending, message, [@location, *caller].uniq
end
run(report) click to toggle source

Run a test in isolation. Any setup and teardown blocks defined for this test case will be run as expected.

You need to provide a Runner instance to handle errors/pending tests/etc.

If the test's block is nil, then the test will be marked as pending and nothing will be run.

# File lib/protest/test_case.rb, line 120
def run(report)
  @report = report
  pending if test.nil?

  begin
    setup
    instance_eval(&test)
  ensure
    teardown
    @report = nil
  end
end

Private Instance Methods

test() click to toggle source
# File lib/protest/test_case.rb, line 179
def test
  @test
end