Module: FunWith::Testing::Assertions::Basics
- Defined in:
- lib/fun_with/testing/assertions/basics.rb
Instance Method Summary collapse
-
#assert_assigns(*args) ⇒ Object
If successful, returns an array of assigned objects.
- #assert_blank(obj, message = "") ⇒ Object
- #assert_doesnt_match(string, regexp, message = "") ⇒ Object
- #assert_equal_length(expected, actual, message = "") ⇒ Object
- #assert_equality_of_methods(*args) ⇒ Object
- #assert_false(actual, message = "") ⇒ Object
-
#assert_greater_than(reference_value, amount, message = "") ⇒ Object
Ick read as “assert greater than 5, <test_value>”.
- #assert_has_instance_method(object, instance_method, message = "object #{object} should respond to #{instance_method.inspect}") ⇒ Object
-
#assert_less_than(reference_value, amount, message = "") ⇒ Object
read as “assert less than 5, <test value>”.
- #assert_matches(string, regexp_or_string, message = "") ⇒ Object
- #assert_negative(actual, message = "") ⇒ Object
- #assert_not_zero(actual, message = "") ⇒ Object
- #assert_one(actual, message = "") ⇒ Object
-
#assert_times_are_close(t1, t2, window = 1, message = "") ⇒ Object
I think “assert_delta_in_range” already does this.
- #assert_true(actual, message = "") ⇒ Object
- #assert_zero(actual, message = "") ⇒ Object
Instance Method Details
#assert_assigns(*args) ⇒ Object
If successful, returns an array of assigned objects. You can do: account, phone_number = assert_assigns(:account, :phone_number) or order = assert_assigns(:order)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 91 def assert_assigns(*args) symbols_assigned = [] symbols_not_assigned = [] for sym in args ((assigns(sym) != nil)? symbols_assigned : symbols_not_assigned) << sym end = ("", "The following variables should have been assigned values by the controller: <?>", symbols_not_assigned.map{|s| "@#{s.to_s}"}.join(", ")) assert_block do symbols_not_assigned.length == 0 end if symbols_assigned.length == 1 assigns(symbols_assigned.first) else symbols_assigned.map{|s| assigns(s)} end end |
#assert_blank(obj, message = "") ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 52 def assert_blank( obj, = "" ) if obj.respond_to?(:blank?) = (, "<?> should be blank.", obj) else = (, "<?> does not respond to :blank? method.", obj) end assert_block do obj.respond_to?(:blank?) && obj.blank? end end |
#assert_doesnt_match(string, regexp, message = "") ⇒ Object
75 76 77 78 79 80 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 75 def assert_doesnt_match( string, regexp, = "") = (, "<?> should not match regex <?>", string, regexp) assert_block do string.match(regexp) ? false : true end end |
#assert_equal_length(expected, actual, message = "") ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 140 def assert_equal_length( expected, actual, = "" ) = ( , "items should be of equal length: expected: <?>, actual: <?>", expected.length, actual.length ) assert_block do expected.length == actual.length end end |
#assert_equality_of_methods(*args) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 148 def assert_equality_of_methods(*args) expected = args[0] actual = args[1] methods = args[2..-1].flatten = "The following methods were not equal: " unequal = [] for method in methods exp = expected.send(method.to_sym) act = actual.send(method.to_sym) unless exp == act unequal << method += "\n\t#{method} (#{exp.inspect},#{act.inspect})" end end assert_block do unequal.blank? end end |
#assert_false(actual, message = "") ⇒ Object
44 45 46 47 48 49 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 44 def assert_false( actual, = "" ) = (, "should be false, not <#{actual}>") assert_block do actual == false end end |
#assert_greater_than(reference_value, amount, message = "") ⇒ Object
Ick read as “assert greater than 5, <test_value>”
114 115 116 117 118 119 120 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 114 def assert_greater_than( reference_value, amount, = "" ) = ("", "second argument <?> should be greater than reference value <?>", amount, reference_value) assert_block do amount > reference_value end end |
#assert_has_instance_method(object, instance_method, message = "object #{object} should respond to #{instance_method.inspect}") ⇒ Object
170 171 172 173 174 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 170 def assert_has_instance_method( object, instance_method, = "object #{object} should respond to #{instance_method.inspect}" ) assert_block( ) do object.instance_methods.include?( instance_method ) end end |
#assert_less_than(reference_value, amount, message = "") ⇒ Object
read as “assert less than 5, <test value>”
123 124 125 126 127 128 129 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 123 def assert_less_than( reference_value, amount, = "" ) = ("", "second argument <?> should be less than reference value <?>", amount, reference_value) assert_block do amount < reference_value end end |
#assert_matches(string, regexp_or_string, message = "") ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 64 def assert_matches( string, regexp_or_string, = "") = (, "<?> should match regex <?>", string, regexp_or_string) assert_block do if regexp_or_string.is_a?(Regexp) string.match(regexp_or_string) ? true : false elsif regexp_or_string.is_a?(String) string.include?(regexp_or_string) end end end |
#assert_negative(actual, message = "") ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 29 def assert_negative( actual, = "" ) = (, "should be negative, not <#{actual}>") assert_block do actual < 0 end end |
#assert_not_zero(actual, message = "") ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 5 def assert_not_zero( actual, = "" ) = (, "should not be zero") assert_block do actual != 0 end end |
#assert_one(actual, message = "") ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 21 def assert_one( actual, = "" ) = (, "should be 1, not <#{actual}>") assert_block do actual == 1 end end |
#assert_times_are_close(t1, t2, window = 1, message = "") ⇒ Object
I think “assert_delta_in_range” already does this
132 133 134 135 136 137 138 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 132 def assert_times_are_close( t1, t2, window = 1, = "") = (, "times should be within ? second of each other.", window) assert_block do (t1 - t2).abs < window end end |
#assert_true(actual, message = "") ⇒ Object
37 38 39 40 41 42 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 37 def assert_true( actual, = "" ) = (, "should be true, not <#{actual}>") assert_block do actual == true end end |
#assert_zero(actual, message = "") ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/fun_with/testing/assertions/basics.rb', line 13 def assert_zero( actual, = "" ) = (, "should be zero, not <#{actual}>") assert_block do actual == 0 end end |