From 848478356cd33c04f648b7ca95bc2b2d15786715 Mon Sep 17 00:00:00 2001 From: Emily Behlmann Date: Sun, 30 Oct 2016 17:03:57 -0500 Subject: [PATCH] adding model validations, tests and factory for answer, plus question factory bc of dependency. resolves #3 --- app/models/answer.rb | 3 +++ spec/examples.txt | 3 +++ spec/factories/answer_factory.rb | 22 ++++++++++++++++++++++ spec/factories/question_factory.rb | 23 +++++++++++++++++++++++ spec/models/answer_spec.rb | 17 +++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 spec/factories/answer_factory.rb create mode 100644 spec/factories/question_factory.rb diff --git a/app/models/answer.rb b/app/models/answer.rb index e510c44..17775e3 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -12,4 +12,7 @@ class Answer < ActiveRecord::Base belongs_to :question + + validates :question_id, presence: true + validates :text, presence: true end diff --git a/spec/examples.txt b/spec/examples.txt index 63dc2f9..bc372ab 100644 --- a/spec/examples.txt +++ b/spec/examples.txt @@ -1,5 +1,8 @@ example_id | status | run_time | --------------------------------------- | ------ | --------------- | ./spec/models/admin_user_spec.rb[1:1:1] | passed | 0.0534 seconds | +./spec/models/answer_spec.rb[1:1] | passed | 0.10162 seconds | +./spec/models/answer_spec.rb[1:2:1] | passed | 0.01378 seconds | +./spec/models/answer_spec.rb[1:2:2] | passed | 0.01236 seconds | ./spec/models/ballot_spec.rb[1:1:1] | passed | 0.04902 seconds | ./spec/models/ballot_spec.rb[1:1:2] | passed | 0.0089 seconds | diff --git a/spec/factories/answer_factory.rb b/spec/factories/answer_factory.rb new file mode 100644 index 0000000..501743a --- /dev/null +++ b/spec/factories/answer_factory.rb @@ -0,0 +1,22 @@ +# == Schema Information +# +# Table name: answers +# +# id :integer not null, primary key +# text :text +# info :text +# question_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + +require 'faker' + +FactoryGirl.define do + factory :answer do + text Faker::Lorem.sentence + + # Associations + question + end +end \ No newline at end of file diff --git a/spec/factories/question_factory.rb b/spec/factories/question_factory.rb new file mode 100644 index 0000000..06471ce --- /dev/null +++ b/spec/factories/question_factory.rb @@ -0,0 +1,23 @@ +# == Schema Information +# +# Table name: questions +# +# id :integer not null, primary key +# text :text +# summary :text +# friendly_name :string +# ballot_id :integer +# created_at :datetime not null +# updated_at :datetime not null +# + +require 'faker' + +FactoryGirl.define do + factory :question do + text Faker::Lorem.sentence + + # Associations + ballot + end +end \ No newline at end of file diff --git a/spec/models/answer_spec.rb b/spec/models/answer_spec.rb index 8eeaeb8..bda9e7d 100644 --- a/spec/models/answer_spec.rb +++ b/spec/models/answer_spec.rb @@ -13,4 +13,21 @@ require 'spec_helper' RSpec.describe Answer do + it "has a valid factory" do + expect(FactoryGirl.create(:answer)).to be_valid + end + + describe 'validates' do + before(:each) { @answer = build(:answer) } + + it "is invalid without an associated question id" do + @answer.question_id = nil + expect(@answer).not_to be_valid + end + + it "is invalid without text" do + @answer.text = nil + expect(@answer).not_to be_valid + end + end end