Skip to content

Commit 184991b

Browse files
committed
Use a single custom error class: ExpectationNotMet
1 parent f8258e5 commit 184991b

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ I will use TMF in my projects and discover if such a minimalistic tool can be pr
2323
# => true
2424

2525
assert(1 + 1, :equals => 3)
26-
# => TMF::AssertionFailed: Expected 2 to equal 3
26+
# => TMF::ExpectationNotMet: Expected 2 to equal 3
2727

2828
assert(Object.foo, :equals => :bar)
2929
# => NoMethodError: undefined method `foo' for Object:Class
@@ -108,7 +108,7 @@ And you also have a file `PROJECT_ROOT/test/foo_test.rb` with the following:
108108

109109
# failing test
110110
assert(f.class, :equals => 'Bar')
111-
# => TMF::AssertionFailed: Expected Foo to equal Bar
111+
# => TMF::ExpectationNotMet: Expected Foo to equal Bar
112112

113113
# stub with passing test
114114
stub(f, :method => :class, :return => 'Bar') do
@@ -120,7 +120,7 @@ And you also have a file `PROJECT_ROOT/test/foo_test.rb` with the following:
120120
stub(f, :method => :bar, :return => :baz) do
121121
assert(f.bar, :equals => :snafu)
122122
end
123-
# TMF::AssertionFailed: Expected baz to equal snafu
123+
# TMF::ExpectationNotMet: Expected baz to equal snafu
124124

125125
# testing a raised error
126126
begin

tmf.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,14 @@
2121
# THE SOFTWARE.
2222

2323
module TMF
24-
class AssertionFailed < StandardError
25-
def initialize(a, b)
26-
super("Expected #{a} to equal #{b}")
27-
end
28-
end
29-
3024
class ExpectationNotMet < StandardError
31-
def initialize(o, method)
32-
super("Expected #{o} to receive #{method}")
25+
def initialize(o, method, verb)
26+
super("Expected #{o} to #{verb} #{method}")
3327
end
3428
end
3529

3630
def assert(a, opts)
37-
a == opts[:equals] ? true : raise( AssertionFailed.new(a,opts[:equals]) )
31+
a == opts[:equals] ? true : raise( ExpectationNotMet.new(a,opts[:equals], 'equal') )
3832
end
3933

4034
def stub(o, opts)
@@ -44,7 +38,7 @@ def stub(o, opts)
4438
o.singleton_class.send(:define_method, opts[:method]) { called = 1; opts[:return] }
4539
result = yield if block_given?
4640

47-
raise ExpectationNotMet.new(o, opts[:method]) if opts[:spy] && !called
41+
raise ExpectationNotMet.new(o, opts[:method], 'receive') if opts[:spy] && !called
4842

4943
result
5044
ensure

0 commit comments

Comments
 (0)