Module: Shoulda::ActiveRecord::Assertions

### Home Classes Methods

### In Files

- lib/shoulda/active_record/assertions.rb

### Methods

- #assert_bad_value
- #assert_good_value
- #assert_save
- #assert_valid

### Files

- CONTRIBUTION_GUIDELINES.rdoc
- README.rdoc

### Class/Module Index

Quicksearch

- Shoulda
- Shoulda::ActionController
- Shoulda::ActionController::Macros
- Shoulda::ActionController::Matchers
- Shoulda::ActionMailer
- Shoulda::ActionMailer::Assertions
- Shoulda::ActionMailer::Matchers
- Shoulda::ActiveRecord
- Shoulda::ActiveRecord::Assertions
- Shoulda::ActiveRecord::Helpers
- Shoulda::ActiveRecord::Macros
- Shoulda::ActiveRecord::Matchers
- Shoulda::Assertions
- Shoulda::ClassMethods
- Shoulda::Helpers
- Shoulda::InstanceMethods
- Shoulda::Macros
- ActionController
- Test

No matching classes.

# Shoulda::ActiveRecord::Assertions

### Public Instance Methods

assert_bad_value(object_or_klass, attribute, value, error_message_to_expect = nil) click to toggle source

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")

    # File lib/shoulda/active_record/assertions.rb, line 59
    def assert_bad_value(object_or_klass, attribute, value,
                         error_message_to_expect = nil)
      object = get_instance_of(object_or_klass)
      matcher = allow_value(value).
                  for(attribute).
                  with_message(error_message_to_expect)
      assert_rejects(matcher, object)
    end

assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = nil) click to toggle source

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")

    # File lib/shoulda/active_record/assertions.rb, line 35
    def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = nil)
      object = get_instance_of(object_or_klass)
      matcher = allow_value(value).
                  for(attribute).
                  with_message(error_message_to_avoid)
      assert_accepts(matcher, object)
    end

assert_save(obj) click to toggle source

Asserts that the given object can be saved

    assert_save User.new(params)

    # File lib/shoulda/active_record/assertions.rb, line 7
    def assert_save(obj)
      assert obj.save, "Errors: #{pretty_error_messages obj}"
      obj.reload
    end

assert_valid(obj) click to toggle source

Asserts that the given object is valid

    assert_valid User.new(params)

    # File lib/shoulda/active_record/assertions.rb, line 15
    def assert_valid(obj)
      assert obj.valid?, "Errors: #{pretty_error_messages obj}"
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.