When an user submits his work, I validate that he does not submit more than once to an assignment:
def cannot_submit_more_than_once_for_same_assignment
if submitter.submissions.map(&:assignment).map(&:id).include?(assignment.id)
errors.add(:base, "You cannot submit to an assignment more than once.")
end
end
This method assumes that the attributes submitter and assignment of the submission are not nil. This works fine in practice since the form forces these two attributes to be not nil.
However, this method clashes with some tests, such as:
describe Submission do
describe "must have an associated assignment" do
before { hw.assignment = nil }
it { should be_invalid }
end
end
This test crashes because assignment is set to nil, leading to error within the validation method.
This causes me to wonder whether my validation method shouldn't make the assumption that some attributes (submitter and assignment in this case) are present? On the other hand, if I check if !submitter.nil? in validation, it makes the code quite obtuse (e.g. why check for nil here when according to business logic it should never be there?)
So should my validation makes the assumption or not?
Aucun commentaire:
Enregistrer un commentaire