Module: FunWith::Testing::Assertions::ActiveRecord

Defined in:
lib/fun_with/testing/assertions/active_record.rb

Instance Method Summary collapse

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 )
  message = build_message( "", "<?> should have an error on the <?> field.", record, _field )
  assert_block message  do
    !record.errors[_field].blank?
  end

  unless error_says.blank?
    message = build_message( "", "<?> should have an error on the <?> field that says <?>.", record, _field, error_says )
    assert_block message 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, message = "" )
  message = build_message( message, "#{record.class.name} record should have errors.")

  assert_block message 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, message = "" )
  record.valid?
  message = build_message( message,
                           "#{record.class.name} record should have no errors.  Errors: ?",
                           record.errors.map{ |k,v| "[#{k} : #{v}]"}.join(", ")
                           )

  assert_block message 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, message = "")
  not_record_message = build_message(message, "<?> is not an ActiveRecord::Base object.", record)
  new_record_message = build_message(message, "<?> should not be a new record in order to use assert_destroy().", record)
  full_message = build_message(message, "<?> should have been destroyed.", record)

  assert_block not_record_message do
    record.is_a?(ActiveRecord::Base)
  end

  assert_block new_record_message do
    record.new_record? == false
  end

  assert_block full_message 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, message = "")
  result = record.save

  message = "Record #{record} did not save properly.  "
  message += record.errors.map{ |k,v| "#{k} : #{v}"}.join(", ")

  assert_block message 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 Test::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