views:

245

answers:

1

Seems like this should be findable with a couple of Google searches...but no luck.

I'm looking for an elegant approach to testing validates_associated in my models, such as...

class Network < ActiveRecord::Base
...
  validates_associated :transporter
...
end

And the test:

class NetworkTest < ActiveSupport::TestCase
  test 'should not create network without valid transporter' do
    network = Factory.build(:network)
    assert...?
  end
end
A: 

Factory.build may not run the validation test (the validation test runs on save, not create).

However, in general for validations you would do something like

assert !network.valid? assert network.errors.invalid?(:transporter)

Note that I tend to do this at the model test (unit test) levels -- I check the result of something being invalid at the function and integration levels.

Kathy Van Stone