Module ThoughtBot::Shoulda::General
In: lib/shoulda/general.rb

Methods

Classes and Modules

Module ThoughtBot::Shoulda::General::ClassMethods

Public Instance methods

Asserts that an Active Record model invalidates the passed value by making sure the error_message_to_expect is contained within the list of errors for that attribute.

  assert_bad_value(User.new, :email, "invalid")
  assert_bad_value(User.new, :ssn, "123", /length/)

If a class is passed as the first argument, a new object will be instantiated before the assertion. If an instance variable exists with the same name as the class (underscored), that object will be used instead.

  assert_bad_value(User, :email, "invalid")

  @product = Product.new(:tangible => true)
  assert_bad_value(Product, :price, "0")

[Source]

     # File lib/shoulda/general.rb, line 220
220:       def assert_bad_value(object_or_klass, attribute, value, error_message_to_expect = /invalid/)
221:         object = get_instance_of(object_or_klass)
222:         object.send("#{attribute}=", value)
223:         assert !object.valid?, "#{object.class} allowed #{value.inspect} as a value for #{attribute}"
224:         assert object.errors.on(attribute), "There are no errors on #{attribute} after being set to #{value.inspect}"
225:         assert_contains(object.errors.on(attribute), error_message_to_expect, "when set to #{value.inspect}")
226:       end

Asserts that the given collection contains item x. If x is a regular expression, ensure that at least one element from the collection matches x. extra_msg is appended to the error message if the assertion fails.

  assert_contains(['a', '1'], /\d/) => passes
  assert_contains(['a', '1'], 'a') => passes
  assert_contains(['a', '1'], /not there/) => fails

[Source]

     # File lib/shoulda/general.rb, line 114
114:       def assert_contains(collection, x, extra_msg = "")
115:         collection = [collection] unless collection.is_a?(Array)
116:         msg = "#{x.inspect} not found in #{collection.to_a.inspect} #{extra_msg}"
117:         case x
118:         when Regexp: assert(collection.detect { |e| e =~ x }, msg)
119:         else         assert(collection.include?(x), msg)
120:         end
121:       end

Asserts that no ActionMailer mails were delivered

 assert_did_not_send_email

[Source]

     # File lib/shoulda/general.rb, line 175
175:       def assert_did_not_send_email
176:         msg = "Sent #{ActionMailer::Base.deliveries.size} emails.\n"
177:         ActionMailer::Base.deliveries.each { |m| msg << "  '#{m.subject}' sent to #{m.to.to_sentence}\n" }
178:         assert ActionMailer::Base.deliveries.empty?, msg
179:       end

Asserts that the given collection does not contain item x. If x is a regular expression, ensure that none of the elements from the collection match x.

[Source]

     # File lib/shoulda/general.rb, line 125
125:       def assert_does_not_contain(collection, x, extra_msg = "")
126:         collection = [collection] unless collection.is_a?(Array)
127:         msg = "#{x.inspect} found in #{collection.to_a.inspect} " + extra_msg
128:         case x
129:         when Regexp: assert(!collection.detect { |e| e =~ x }, msg)
130:         else         assert(!collection.include?(x), msg)
131:         end
132:       end

Asserts that an Active Record model validates with the passed value by making sure the error_message_to_avoid is not contained within the list of errors for that attribute.

  assert_good_value(User.new, :email, "user@example.com")
  assert_good_value(User.new, :ssn, "123456789", /length/)

If a class is passed as the first argument, a new object will be instantiated before the assertion. If an instance variable exists with the same name as the class (underscored), that object will be used instead.

  assert_good_value(User, :email, "user@example.com")

  @product = Product.new(:tangible => false)
  assert_good_value(Product, :price, "0")

[Source]

     # File lib/shoulda/general.rb, line 197
197:       def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = //)
198:         object = get_instance_of(object_or_klass)
199:         object.send("#{attribute}=", value)
200:         object.valid?
201:         assert_does_not_contain(object.errors.on(attribute), error_message_to_avoid, "when set to #{value.inspect}")
202:       end

Asserts that two arrays contain the same elements, the same number of times. Essentially ==, but unordered.

  assert_same_elements([:a, :b, :c], [:c, :a, :b]) => passes

[Source]

     # File lib/shoulda/general.rb, line 97
 97:       def assert_same_elements(a1, a2, msg = nil)
 98:         [:select, :inject, :size].each do |m|
 99:           [a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a.inspect} is an array?  It doesn't respond to #{m}.") }
100:         end
101: 
102:         assert a1h = a1.inject({}) { |h,e| h[e] = a1.select { |i| i == e }.size; h }
103:         assert a2h = a2.inject({}) { |h,e| h[e] = a2.select { |i| i == e }.size; h }
104: 
105:         assert_equal(a1h, a2h, msg)
106:       end

Asserts that the given object can be saved

 assert_save User.new(params)

[Source]

     # File lib/shoulda/general.rb, line 137
137:       def assert_save(obj)
138:         assert obj.save, "Errors: #{pretty_error_messages obj}"
139:         obj.reload
140:       end

Asserts that an email was delivered. Can take a block that can further narrow down the types of emails you‘re expecting.

 assert_sent_email

Passes if ActionMailer::Base.deliveries has an email

 assert_sent_email do |email|
   email.subject =~ /hi there/ && email.to.include?('none@none.com')
 end

Passes if there is an email with subject containing ‘hi there’ and ‘none@none.com’ as one of the recipients.

[Source]

     # File lib/shoulda/general.rb, line 163
163:       def assert_sent_email
164:         emails = ActionMailer::Base.deliveries
165:         assert !emails.empty?, "No emails were sent"
166:         if block_given?
167:           matching_emails = emails.select {|email| yield email }
168:           assert !matching_emails.empty?, "None of the emails matched."
169:         end
170:       end

Asserts that the given object is valid

 assert_valid User.new(params)

[Source]

     # File lib/shoulda/general.rb, line 145
145:       def assert_valid(obj)
146:         assert obj.valid?, "Errors: #{pretty_error_messages obj}"
147:       end

[Source]

     # File lib/shoulda/general.rb, line 228
228:       def pretty_error_messages(obj)
229:         obj.errors.map { |a, m| "#{a} #{m} (#{obj.send(a).inspect})" }
230:       end

Prints a message to stdout, tagged with the name of the calling method.

[Source]

    # File lib/shoulda/general.rb, line 90
90:       def report!(msg = "")
91:         puts("#{caller.first}: #{msg}")
92:       end

[Validate]