I have a the following has_many model associations in my app:
User < Company < Deed < Subtransaction,
where Deed accepts_nested_attributes_for :subtransactions. I wish to test my Model validations using Minitest and fixtures.
I have trouble, however, testing the nested attributes for validity. For example , If I clear all nested attributes using
@deed.subtransactions.clear I correctly get a test response that the model is not valid.
However
@deed.subtransactions.first.num_shares = " " does not seem to work.
How do I properly test these nested attributes for validity?
My test:
class DeedTest < ActiveSupport::TestCase
def setup
@user = users(:dagobert)
@newco = companies(:newco)
params = { :deed =>
{
:date => deeds(:inc_new).date,
:subtransactions_attributes =>
{ '1' =>
{
:shareholder_name => "Shareholder 1",
:num_shares => subtransactions(:new_subt1).num_shares
},
'2' =>
{
:shareholder_name => "Shareholder 2",
:num_shares => subtransactions(:new_subt2).num_shares
}
}
}
}
@deed = @newco.deeds.new(params[:deed])
end
# test: does not raise any error messages
test "num_shares should be present for a subtransaction" do
@deed.subtransactions.first.num_shares = nil
assert_not @deed.valid?
end
# test passing: The params are submitted correctly and pass validation
test "fixture values should be valid" do
assert @deed.valid?
end
# test passing: I can test the validity of deed attributes
test "date should be present" do
@deed.date = " "
assert_not @deed.valid?
end
# test passing: when I clear @deed.subtransactions the object is no longer valid
test "a subtransaction should be present" do
@deed.subtransactions.clear
assert_not @deed.valid?
end
end
Aucun commentaire:
Enregistrer un commentaire