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
f4984945 » rmm5t 2008-09-03 Added a `should_fail` macro... 1 module Thoughtbot
2 module Shoulda
3 class << self
4 attr_accessor :expected_exceptions
5 end
6
7 # Enables the core shoulda test suite to test for failure scenarios. For
8 # example, to ensure that a set of test macros should fail, do this:
9 #
10 # should_fail do
11 # should_require_attributes :comments
12 # should_protect_attributes :name
13 # end
14 def should_fail(&block)
15 context "should fail when trying to run:" do
16 Shoulda.expected_exceptions = [Test::Unit::AssertionFailedError]
17 yield block
18 Shoulda.expected_exceptions = nil
19 end
20 end
21
22 class Context
23 # alias_method_chain hack to allow the should_fail macro to work
24 def should_with_failure_scenario(name, options = {}, &block)
25 if Shoulda.expected_exceptions
26 expected_exceptions = Shoulda.expected_exceptions
27 failure_block = lambda { assert_raise(*expected_exceptions, &block.bind(self)) }
28 end
29 should_without_failure_scenario(name, options, &(failure_block || block))
30 end
31 alias_method_chain :should, :failure_scenario
32 end
33 end
34 end