Module: FunWith::Testing::Assertions::Basics

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

Instance Method Summary collapse

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)



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fun_with/testing/assertions/basics.rb', line 107

def assert_assigns(*args)
  symbols_assigned = []
  symbols_not_assigned = []

  for sym in args
    ((assigns(sym) != nil)? symbols_assigned : symbols_not_assigned) << sym
  end

  message = build_message("", "The following variables should have been assigned values by the controller: <?>", symbols_not_assigned.map{|s| "@#{s.to_s}"}.join(", "))

  safe_assert_block message 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



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fun_with/testing/assertions/basics.rb', line 68

def assert_blank( obj, message = "" )
  if obj.respond_to?(:blank?)
    full_message = build_message(message, "<?> should be blank.", obj)
  else
    full_message = build_message(message, "<?> does not respond to :blank? method.", obj)
  end 
  
  safe_assert_block full_message do
    obj.respond_to?(:blank?) && obj.blank?
  end
end

#assert_doesnt_match(string, regexp, message = "") ⇒ Object



91
92
93
94
95
96
# File 'lib/fun_with/testing/assertions/basics.rb', line 91

def assert_doesnt_match( string, regexp, message = "")
  full_message = build_message(message, "<?> should not match regex <?>", string, regexp)
  safe_assert_block full_message do
    string.match(regexp) ? false : true
  end
end

#assert_equal_length(expected, actual, message = "") ⇒ Object



156
157
158
159
160
161
162
# File 'lib/fun_with/testing/assertions/basics.rb', line 156

def assert_equal_length( expected, actual, message = "" )
  message = build_message( message, "items should be of equal length: expected: <?>, actual: <?>", expected.length, actual.length )
  
  safe_assert_block message do
    expected.length == actual.length
  end
end

#assert_equality_of_methods(*args) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/fun_with/testing/assertions/basics.rb', line 164

def assert_equality_of_methods(*args)
  expected = args[0]
  actual = args[1]
  methods = args[2..-1].flatten
  message = "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
      message += "\n\t#{method} (#{exp.inspect},#{act.inspect})"
    end
  end

  safe_assert_block message 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, message = "" )
  message = build_message(message, "should be false, not <#{actual}>")
  safe_assert_block message do
    actual == false
  end
end

#assert_greater_than(reference_value, amount, message = "") ⇒ Object

Ick read as “assert greater than 5, <test_value>”



130
131
132
133
134
135
136
# File 'lib/fun_with/testing/assertions/basics.rb', line 130

def assert_greater_than( reference_value, amount, message = "" )
  message = build_message("", "second argument <?> should be greater than reference value <?>", amount, reference_value)

  safe_assert_block message do
    amount > reference_value
  end
end

#assert_has_instance_method(object, instance_method, message = "object #{object} should respond to #{instance_method.inspect}") ⇒ Object



186
187
188
189
190
# File 'lib/fun_with/testing/assertions/basics.rb', line 186

def assert_has_instance_method( object, instance_method, message = "object #{object} should respond to #{instance_method.inspect}" )
  safe_assert_block( message ) 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>”



139
140
141
142
143
144
145
# File 'lib/fun_with/testing/assertions/basics.rb', line 139

def assert_less_than( reference_value, amount, message = "" )
  message = build_message("", "second argument <?> should be less than reference value <?>", amount, reference_value)

  safe_assert_block message do
    amount < reference_value
  end
end

#assert_matches(string, regexp_or_string, message = "") ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/fun_with/testing/assertions/basics.rb', line 80

def assert_matches( string, regexp_or_string, message = "")
  full_message = build_message(message, "<?> should match regex <?>", string, regexp_or_string)
  safe_assert_block full_message 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, message = "" )
  message = build_message(message, "should be negative, not <#{actual}>")

  safe_assert_block message do
    actual < 0
  end
end

#assert_nil(actual, message = "") ⇒ Object



52
53
54
55
56
57
# File 'lib/fun_with/testing/assertions/basics.rb', line 52

def assert_nil( actual, message = "" )
  message = build_message(message, "should be nil, not <#{actual}>")
  safe_assert_block message do
    actual == nil
  end
end

#assert_not_nil(actual, message = "") ⇒ Object Also known as: refute_nil



59
60
61
62
63
64
# File 'lib/fun_with/testing/assertions/basics.rb', line 59

def assert_not_nil( actual, message = "" )
  message = build_message(message, "should not be nil")
  safe_assert_block message do
    actual != nil
  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, message = "" )
  message = build_message(message, "should not be zero")

  safe_assert_block message do
    actual != 0
  end
end

#assert_nothing_raised(message = "", &block) ⇒ Object



192
193
194
195
196
197
# File 'lib/fun_with/testing/assertions/basics.rb', line 192

def assert_nothing_raised( message = "", &block )
  safe_assert_block( message ) do
    result = true
    result
  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, message = "" )
  message = build_message(message, "should be 1, not <#{actual}>")

  safe_assert_block message do
    actual == 1
  end
end

#assert_times_are_close(t1, t2, window = 1, message = "") ⇒ Object

I think “assert_delta_in_range” already does this



148
149
150
151
152
153
154
# File 'lib/fun_with/testing/assertions/basics.rb', line 148

def assert_times_are_close( t1, t2, window = 1, message = "")
  message = build_message(message, "times should be within ? second of each other.", window)

  safe_assert_block message 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, message = "" )
  message = build_message(message, "should be true, not <#{actual}>")
  safe_assert_block message 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, message = "" )
  message = build_message(message, "should be zero, not <#{actual}>")

  safe_assert_block message do
    actual == 0
  end
end