Module: TestPlus::Extensions
- Defined in:
- lib/test_plus/extensions.rb
Instance Method Summary collapse
-
#assert_created(model) ⇒ Object
Tests that a model was created properly: That no errors were reported, that the model is listed as valid, and that it’s no longer flagged as a new record.
-
#assert_equal_reduced(a, b, reduce, message = nil, &block) ⇒ Object
Tests that two lists of objects contain the same values when calling a method on the itens in question, if they respond to it.
-
#assert_errors_on(model, *attrs) ⇒ Object
Tests that the given model has errors on the specified attributes.
-
#assert_exception_raised(exception_class = nil, error_text = nil, message = nil, &block) ⇒ Object
Tests that an exceeption is raised.
-
#assert_mapping(map) ⇒ Object
Tests a number of cases simultaneously using a key-value Hash to map inputs to expected outputs.
-
#assert_not_created(model) ⇒ Object
(also: #refute_created)
Tests that a model was not created by verifying that it has not been saved.
Instance Method Details
#assert_created(model) ⇒ Object
Tests that a model was created properly: That no errors were reported, that the model is listed as valid, and that it’s no longer flagged as a new record.
7 8 9 10 11 12 |
# File 'lib/test_plus/extensions.rb', line 7 def assert_created(model) assert(model, "Model was not defined") assert_equal([ ], model.errors.) assert(model.valid?, "Model failed to validate") assert(!model.new_record?, "Model is still a new record") end |
#assert_equal_reduced(a, b, reduce, message = nil, &block) ⇒ Object
Tests that two lists of objects contain the same values when calling a method on the itens in question, if they respond to it. Those items that do not respond to the method are treated as-is.
40 41 42 43 44 45 46 47 |
# File 'lib/test_plus/extensions.rb', line 40 def assert_equal_reduced(a, b, reduce, = nil, &block) assert_equal( TestPlus::Support.reduce_with(a, reduce).to_a.sort, TestPlus::Support.reduce_with(b, reduce).to_a.sort, , &block ) end |
#assert_errors_on(model, *attrs) ⇒ Object
Tests that the given model has errors on the specified attributes. Any additional errors will be reported as anomalous, and any missing ones will be flagged as well.
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/test_plus/extensions.rb', line 24 def assert_errors_on(model, *attrs) found_attrs = [ ] model.errors.each do |attr, error| found_attrs << attr end assert_equal( attrs.flatten.collect(&:to_s).sort, found_attrs.uniq.collect(&:to_s).sort ) end |
#assert_exception_raised(exception_class = nil, error_text = nil, message = nil, &block) ⇒ Object
Tests that an exceeption is raised. An optional exception class can be provided to ensure that the exception is of the correct type.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/test_plus/extensions.rb', line 79 def assert_exception_raised(exception_class = nil, error_text = nil, = nil, &block) exception_raised = nil yield rescue Object => exception_raised if (exception_class) assert( exception_raised.is_a?(exception_class), || "Exception raised is not of type #{exception_class}, instead is #{exception_raised.class}" ) else assert(true) end if (error_text) assert_equal( error_text, exception_raised.to_s, || "Exception raised had incorrect text" ) end else if (exception_class) flunk( || "Exception of type #{exception_class} was not raised") else flunk( || "An exception was not raised") end end |
#assert_mapping(map) ⇒ Object
Tests a number of cases simultaneously using a key-value Hash to map inputs to expected outputs. The keys are supplied to a block and the result of that block is compared to the expected value. Any mistakes are reported as failures.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/test_plus/extensions.rb', line 53 def assert_mapping(map) result_map = map.each_with_object({ }) do |(k,_), h| if (k and k.respond_to?(:freeze)) k = k.freeze end h[k] = yield(k) end differences = result_map.each_with_object([ ]) do |(k,v), a| next unless (v == map[k]) a << k end assert_equal( map, result_map, differences.collect do |s| "Input: #{s.inspect}\n Expected: #{map[s].inspect}\n Result: #{result_map[s].inspect}\n" end.join('') ) end |
#assert_not_created(model) ⇒ Object Also known as: refute_created
Tests that a model was not created by verifying that it has not been saved.
15 16 17 18 |
# File 'lib/test_plus/extensions.rb', line 15 def assert_not_created(model) assert(model, "Model was not defined") assert(model.new_record?, "Model has been saved") end |