diff --git a/app/models/question_format.rb b/app/models/question_format.rb index b8ede7c..4a66b0d 100644 --- a/app/models/question_format.rb +++ b/app/models/question_format.rb @@ -3,6 +3,9 @@ #associations between tables has_many :questions + + validates :title, presence: true, uniqueness: true + ## # gives the title of the question_format # diff --git a/test/unit/question_format_test.rb b/test/unit/question_format_test.rb index e69de29..d936205 100644 --- a/test/unit/question_format_test.rb +++ b/test/unit/question_format_test.rb @@ -0,0 +1,41 @@ +require 'test_helper' + +class QuestionFormatTest < ActiveSupport::TestCase + + def setup + @question = Question.first + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not QuestionFormat.new.valid? + assert_not QuestionFormat.new(description: 'Random Number').valid? + + assert QuestionFormat.new(title: 'Random').valid? + assert QuestionFormat.new(title: 'Random', description: 'Random Number').valid? + end + + # --------------------------------------------------- + test "abbreviation must be unique" do + assert_not QuestionFormat.new(title: QuestionFormat.first.title).valid? + end + + # --------------------------------------------------- + test "can CRUD" do + qf = QuestionFormat.create(title: 'Random', description: 'Random Number') + assert_not qf.id.nil?, "was expecting to be able to create a new QuestionFormat : #{qf.errors.collect{ |e| e }.join(', ')}" + + qf.description = 'Random String' + qf.save! + assert_equal 'Random String', qf.reload.description, "was expecting the description to have been updated!" + + assert qf.destroy!, "Was unable to delete the QuestionFormat!" + end + + # --------------------------------------------------- + test "can manage has_many relationship with Questions" do + qf = QuestionFormat.new(title: 'Random', description: 'Random Number') + verify_has_many_relationship(qf, @question, qf.questions.count) + end + +end \ No newline at end of file