Module: FunWith::Testing::Assertions::ActiveRecord
- Defined in:
- lib/fun_with/testing/assertions/active_record.rb
Instance Method Summary collapse
- #assert_an_error_on(record, _field, error_says = nil) ⇒ Object
-
#assert_errors_on(record, message = "") ⇒ Object
TODO: Should be able to say which errors should be present.
- #assert_no_errors_on(record, message = "") ⇒ Object
- #assert_record_destroyed(record, message = "") ⇒ Object
- #assert_record_save(record, message = "") ⇒ Object
- #assert_response_redirect(opts = {}) ⇒ Object
-
#assert_response_success(opts = {}) ⇒ Object
Usage: get(“index”); assert_response_success( :template => “index” ).
Instance Method Details
#assert_an_error_on(record, _field, error_says = nil) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/fun_with/testing/assertions/active_record.rb', line 72 def assert_an_error_on( record, _field, error_says = nil ) = ( "", "<?> should have an error on the <?> field.", record, _field ) assert_block do !record.errors[_field].blank? end unless error_says.blank? = ( "", "<?> should have an error on the <?> field that says <?>.", record, _field, error_says ) assert_block do # puts "Inside field error block" # debugger record.errors[_field].include?(error_says) end end end |
#assert_errors_on(record, message = "") ⇒ Object
TODO: Should be able to say which errors should be present
64 65 66 67 68 69 70 |
# File 'lib/fun_with/testing/assertions/active_record.rb', line 64 def assert_errors_on( record, = "" ) = ( , "#{record.class.name} record should have errors.") assert_block do !record.valid? end end |
#assert_no_errors_on(record, message = "") ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/fun_with/testing/assertions/active_record.rb', line 51 def assert_no_errors_on( record, = "" ) record.valid? = ( , "#{record.class.name} record should have no errors. Errors: ?", record.errors.map{ |k,v| "[#{k} : #{v}]"}.join(", ") ) assert_block do record.valid? end end |
#assert_record_destroyed(record, message = "") ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/fun_with/testing/assertions/active_record.rb', line 88 def assert_record_destroyed( record, = "") = (, "<?> is not an ActiveRecord::Base object.", record) = (, "<?> should not be a new record in order to use assert_destroy().", record) = (, "<?> should have been destroyed.", record) assert_block do record.is_a?(ActiveRecord::Base) end assert_block do record.new_record? == false end assert_block do record.class.find_by_id(record.id) == nil end end |
#assert_record_save(record, message = "") ⇒ Object
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/fun_with/testing/assertions/active_record.rb', line 5 def assert_record_save( record, = "") result = record.save = "Record #{record} did not save properly. " += record.errors.map{ |k,v| "#{k} : #{v}"}.join(", ") assert_block do result end end |
#assert_response_redirect(opts = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/fun_with/testing/assertions/active_record.rb', line 33 def assert_response_redirect( opts = {} ) assert_block "@response is nil" do !@response.nil? end if @response.error? || @response.client_error? puts @response.body puts "Flash:" + @response.flash.map{|k,v| "#{k} : #{v}"}.join(', ') debugger nil elsif @response.success? puts "OOPS: should have redirected. Instead went to #{@response.template.action_name}, flash: #{@response.flash.inspect}" end assert_response :redirect assert_redirected_to opts[:to] if opts[:to] end |
#assert_response_success(opts = {}) ⇒ Object
Usage: get(“index”); assert_response_success( :template => “index” )
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fun_with/testing/assertions/active_record.rb', line 18 def assert_response_success( opts = {} ) assert_block "@response is nil" do !@response.nil? end if @response.error? puts @response.body elsif @response.redirect? raise Minitest::Unit::AssertionFailedError.new( "Expected success, was redirect to #{@response.redirected_to} with flash #{flash}" ) end assert_response :success assert_template opts[:template] if opts[:template] end |