public
Fork of thoughtbot/shoulda
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/rmm5t/shoulda.git
shoulda / test / fail_macros.rb
100644 35 lines (32 sloc) 1.136 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Thoughtbot
  module Shoulda
    class << self
      attr_accessor :expected_exceptions
    end
 
    # 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
 
    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
end