Sha256: fe2497974804d4e5c6a679bdcb2dc33708ca68a5607c6dd16fe2d167fd7850a5

Contents?: true

Size: 1.27 KB

Versions: 39

Compression:

Stored size: 1.27 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_validate_presence_of :comments
    #     should_not_allow_mass_assignment_of :name
    #   end
    def should_fail(&block)
      context "should fail when trying to run:" do
        if defined?(Test::Unit::AssertionFailedError)
          failures = [Test::Unit::AssertionFailedError]
        elsif defined?(MiniTest::Assertion)
          failures = [MiniTest::Assertion]
        end
        Shoulda.expected_exceptions = failures
        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

39 entries across 39 versions & 11 rubygems

Version Path
auser-poolparty-1.3.0 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.1 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.10 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.11 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.12 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.13 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.14 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.15 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.16 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.17 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.2 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.3 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.4 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.5 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.6 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.7 vendor/gems/shoulda/test/fail_macros.rb
auser-poolparty-1.3.8 vendor/gems/shoulda/test/fail_macros.rb
fairchild-poolparty-1.3.17 vendor/gems/shoulda/test/fail_macros.rb
fairchild-poolparty-1.3.5 vendor/gems/shoulda/test/fail_macros.rb
iGEL-shoulda-2.10.2 test/fail_macros.rb