Skip to content

Commit

Permalink
adding model validations, tests and factory for answer, plus question…
Browse files Browse the repository at this point in the history
… factory bc of dependency. resolves openwichita#3
  • Loading branch information
Emily Behlmann authored and Emily Behlmann committed Oct 30, 2016
1 parent ee097dc commit 8484783
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@

class Answer < ActiveRecord::Base
belongs_to :question

validates :question_id, presence: true
validates :text, presence: true
end
3 changes: 3 additions & 0 deletions spec/examples.txt
Original file line number Diff line number Diff line change
@@ -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 |
22 changes: 22 additions & 0 deletions spec/factories/answer_factory.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions spec/factories/question_factory.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions spec/models/answer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8484783

Please sign in to comment.