Sha256: a74d3fbc629fdff133d3e85209e58a5fff7515feabe3aa7235fb9b5fdae32075

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

module Shoulda
  class << self
    attr_accessor :expected_exceptions
  end

  module ClassMethods
    # Enables the core shoulda test suite to test for failure scenarios.  For
    # example, to ensure that a set of test macros should fail, do this:
    #
    #   should_fail do
    #     should_require_attributes :comments
    #     should_protect_attributes :name
    #   end
    def should_fail(&block)
      context "should fail when trying to run:" do
        Shoulda.expected_exceptions = [Test::Unit::AssertionFailedError]
        yield block
        Shoulda.expected_exceptions = nil
      end
    end
  end

  class Context
    # alias_method_chain hack to allow the should_fail macro to work
    def should_with_failure_scenario(name, options = {}, &block)
      if Shoulda.expected_exceptions
        expected_exceptions = Shoulda.expected_exceptions
        failure_block = lambda { assert_raise(*expected_exceptions, &block.bind(self)) }
      end
      should_without_failure_scenario(name, options, &(failure_block || block))
    end
    alias_method_chain :should, :failure_scenario
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ratnikov-shoulda-2.0.6.1 test/fail_macros.rb
ratnikov-shoulda-2.0.6.2 test/fail_macros.rb
ratnikov-shoulda-2.0.6.3 test/fail_macros.rb