riot 0.9.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +7 -0
- data/lib/riot/assertion.rb +1 -1
- data/lib/riot/macros.rb +10 -3
- data/riot.gemspec +1 -1
- data/test/assertion_test.rb +24 -0
- metadata +1 -1
data/README.markdown
CHANGED
@@ -115,6 +115,13 @@ Sometimes, your test raises an exception that you actually expected.
|
|
115
115
|
asserts("invalid data") { @user.save! }.raises(ActiveRecord::RecordInvalid)
|
116
116
|
end
|
117
117
|
|
118
|
+
And if you wanted to check that the exception and message match what you expect:
|
119
|
+
|
120
|
+
context "a new user" do
|
121
|
+
setup { @user = User.new }
|
122
|
+
asserts("invalid data") { @user.save! }.raises(ActiveRecord::RecordInvalid, /has errors/)
|
123
|
+
end
|
124
|
+
|
118
125
|
#### Example: Kind Of
|
119
126
|
|
120
127
|
When you want to test that an expression returns an object of an expected type:
|
data/lib/riot/assertion.rb
CHANGED
@@ -23,7 +23,7 @@ module Riot
|
|
23
23
|
private
|
24
24
|
def actualize(situation, &block)
|
25
25
|
@actual = situation.instance_eval(&block)
|
26
|
-
@default_failure = fail("expected true, not #{@actual.inspect}") unless @actual
|
26
|
+
@default_failure = fail("expected true, not #{@actual.inspect}") unless @actual == true
|
27
27
|
rescue Exception => e
|
28
28
|
@raised = e
|
29
29
|
end
|
data/lib/riot/macros.rb
CHANGED
@@ -17,10 +17,17 @@ module Riot
|
|
17
17
|
# Asserts that the test raises the expected Exception
|
18
18
|
# asserts("test") { raise My::Exception }.raises(My::Exception)
|
19
19
|
# should("test") { raise My::Exception }.raises(My::Exception)
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
# asserts("test") { raise My::Exception, "Foo" }.raises(My::Exception, "Foo")
|
21
|
+
# asserts("test") { raise My::Exception, "Foo Bar" }.raises(My::Exception, /Bar/)
|
22
|
+
def raises(expected, expected_message=nil)
|
23
|
+
actual_error = raised
|
23
24
|
@raised = nil
|
25
|
+
return fail("should have raised #{expected}, but raised nothing") unless actual_error
|
26
|
+
return fail("should have raised #{expected}, not #{error.class}") unless expected == actual_error.class
|
27
|
+
if expected_message && !(actual_error.message =~ %r[#{expected_message}])
|
28
|
+
return fail("expected #{expected_message} for message, not #{actual_error.message}")
|
29
|
+
end
|
30
|
+
true
|
24
31
|
end
|
25
32
|
|
26
33
|
# Asserts that the result of the test equals matches against the proved expression
|
data/riot.gemspec
CHANGED
data/test/assertion_test.rb
CHANGED
@@ -39,6 +39,30 @@ end # nil assertion
|
|
39
39
|
|
40
40
|
context "raises assertion:" do
|
41
41
|
should("raise an Exception") { raise Exception }.raises(Exception)
|
42
|
+
|
43
|
+
should "pass if provided message equals expectation" do
|
44
|
+
Riot::Assertion.new("foo", fake_context) do
|
45
|
+
raise Exception, "I'm a nerd"
|
46
|
+
end.raises(Exception, "I'm a nerd")
|
47
|
+
end.equals(true)
|
48
|
+
|
49
|
+
should "fail if provided message does not equal expectation" do
|
50
|
+
Riot::Assertion.new("foo", fake_context) do
|
51
|
+
raise(Exception, "I'm a nerd")
|
52
|
+
end.raises(Exception, "But I'm not")
|
53
|
+
end.kind_of(Riot::Failure)
|
54
|
+
|
55
|
+
should "pass if provided message matches expectation" do
|
56
|
+
Riot::Assertion.new("foo", fake_context) do
|
57
|
+
raise(Exception, "I'm a nerd")
|
58
|
+
end.raises(Exception, %r[nerd])
|
59
|
+
end.equals(true)
|
60
|
+
|
61
|
+
should "fail if provided message does not match expectation" do
|
62
|
+
Riot::Assertion.new("foo", fake_context) do
|
63
|
+
raise(Exception, "I'm a nerd")
|
64
|
+
end.raises(Exception, %r[foo])
|
65
|
+
end.kind_of(Riot::Failure)
|
42
66
|
end # raises assertion
|
43
67
|
|
44
68
|
context "matching assertion:" do
|