Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a
should_fail
macro to help test failure scenarios in the cor…
…e shoulda tests
- Loading branch information
Showing
3 changed files
with
44 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters