Module: Riot::AssertionMacros
- Defined in:
- lib/riot/assertion_macros.rb
Instance Method Summary collapse
-
#assigns(variable, expected_value = nil) ⇒ Object
Asserts that an instance variable is defined for the result of the assertion.
-
#equals(expected) ⇒ Object
Asserts that the result of the test equals the expected value asserts(“test”) { “foo” }.equals(“foo”) should(“test”) { “foo” }.equals(“foo”).
-
#exists ⇒ Object
Asserts that the result of the test is a non-nil value.
-
#kind_of(expected) ⇒ Object
Asserts that the result of the test is an object that is a kind of the expected type asserts(“test”) { “foo” }.kind_of(String) should(“test”) { “foo” }.kind_of(String).
-
#matches(expected) ⇒ Object
Asserts that the result of the test equals matches against the proved expression asserts(“test”) { “12345” }.matches(/d+/) should(“test”) { “12345” }.matches(/d+/).
-
#nil ⇒ Object
Asserts that the result of the test is nil asserts(“test”) { nil }.nil should(“test”) { nil }.nil.
-
#raises(expected, expected_message = nil) ⇒ Object
Asserts that the test raises the expected Exception asserts(“test”) { raise My::Exception }.raises(My::Exception) should(“test”) { raise My::Exception }.raises(My::Exception).
-
#respond_to(expected) ⇒ Object
Asserts that the result of the test is an object that responds to the given method asserts(“test”) { “foo” }.respond_to(:to_s) should(“test”) { “foo” }.respond_to(:to_s).
Instance Method Details
#assigns(variable, expected_value = nil) ⇒ Object
Asserts that an instance variable is defined for the result of the assertion. Value of instance variable is expected to not be nil
setup { User.new(:email => "[email protected]") }
asserts("foo") { topic }.assigns(:email)
If a value is provided in addition to the variable name, the actual value of the instance variable must equal the expected value
setup { User.new(:emmail => "[email protected]") }
asserts("foo") { topic }.assigns(:email, "[email protected]")
78 79 80 81 82 83 84 85 |
# File 'lib/riot/assertion_macros.rb', line 78 def assigns(variable, expected_value=nil) actual_value = actual.instance_variable_get("@#{variable}") return fail("expected @#{variable} to be assigned a value") if actual_value.nil? unless expected_value.nil? || expected_value == actual_value return fail(%Q[expected @#{variable} to be equal to '#{expected_value}', not '#{actual_value}']) end true end |
#equals(expected) ⇒ Object
Asserts that the result of the test equals the expected value
asserts("test") { "foo" }.equals("foo")
should("test") { "foo" }.equals("foo")
6 7 8 |
# File 'lib/riot/assertion_macros.rb', line 6 def equals(expected) expected == actual || fail("expected #{expected.inspect}, not #{actual.inspect}") end |
#exists ⇒ Object
Asserts that the result of the test is a non-nil value. This is useful in the case where you don’t want to translate the result of the test into a boolean value
asserts("test") { "foo" }.exists
should("test") { 123 }.exists
asserts("test") { "" }.exists
asserts("test") { nil }.exists # This would fail
23 24 25 |
# File 'lib/riot/assertion_macros.rb', line 23 def exists !actual.nil? || fail("expected a non-nil value") end |
#kind_of(expected) ⇒ Object
Asserts that the result of the test is an object that is a kind of the expected type
asserts("test") { "foo" }.kind_of(String)
should("test") { "foo" }.kind_of(String)
58 59 60 |
# File 'lib/riot/assertion_macros.rb', line 58 def kind_of(expected) actual.kind_of?(expected) || fail("expected kind of #{expected}, not #{actual.inspect}") end |
#matches(expected) ⇒ Object
Asserts that the result of the test equals matches against the proved expression
asserts("test") { "12345" }.matches(/\d+/)
should("test") { "12345" }.matches(/\d+/)
50 51 52 53 |
# File 'lib/riot/assertion_macros.rb', line 50 def matches(expected) expected = %r[#{Regexp.escape(expected)}] if expected.kind_of?(String) actual =~ expected || fail("expected #{expected.inspect} to match #{actual.inspect}") end |
#nil ⇒ Object
Asserts that the result of the test is nil
asserts("test") { nil }.nil
should("test") { nil }.nil
13 14 15 |
# File 'lib/riot/assertion_macros.rb', line 13 def nil actual.nil? || fail("expected nil, not #{actual.inspect}") end |
#raises(expected, expected_message = nil) ⇒ Object
Asserts that the test raises the expected Exception
asserts("test") { raise My::Exception }.raises(My::Exception)
should("test") { raise My::Exception }.raises(My::Exception)
You can also check to see if the provided message equals or matches your expectations. The message from the actual raised exception will be converted to a string before any comparison is executed.
asserts("test") { raise My::Exception, "Foo" }.raises(My::Exception, "Foo")
asserts("test") { raise My::Exception, "Foo Bar" }.raises(My::Exception, /Bar/)
asserts("test") { raise My::Exception, ["a", "b"] }.raises(My::Exception, "ab")
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/riot/assertion_macros.rb', line 36 def raises(expected, =nil) actual_error = raised.original_exception @raised = nil return fail("should have raised #{expected}, but raised nothing") unless actual_error return fail("should have raised #{expected}, not #{error.class}") unless expected == actual_error.class if && !(actual_error..to_s =~ %r[#{}]) return fail("expected #{} for message, not #{actual_error.}") end true end |
#respond_to(expected) ⇒ Object
Asserts that the result of the test is an object that responds to the given method
asserts("test") { "foo" }.respond_to(:to_s)
should("test") { "foo" }.respond_to(:to_s)
65 66 67 |
# File 'lib/riot/assertion_macros.rb', line 65 def respond_to(expected) actual.respond_to?(expected) || fail("expected method #{expected.inspect} is not defined") end |