Skip to content

Commit

Permalink
Merge pull request #128 from lsa-mis/LRA-943-room-ready-write-rspec-t…
Browse files Browse the repository at this point in the history
…ests-for-specific-attribute-state-model

Lra 943 room ready write rspec tests for specific attribute state model
  • Loading branch information
britaumich authored Jul 3, 2024
2 parents 0b228a4 + eada17d commit 0ac931c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
17 changes: 17 additions & 0 deletions app/models/specific_attribute_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,21 @@
class SpecificAttributeState < ApplicationRecord
belongs_to :room_state
belongs_to :specific_attribute

validate :checkbox_presence_if_required
validate :quantity_box_presence_if_required

private

def checkbox_presence_if_required
if specific_attribute.need_checkbox && checkbox_value.nil?
errors.add(:checkbox_value, "can't be blank if checkbox is required")
end
end

def quantity_box_presence_if_required
if specific_attribute.need_quantity_box && quantity_box_value.nil?
errors.add(:quantity_box_value, "can't be blank if quantity box is required")
end
end
end
12 changes: 9 additions & 3 deletions spec/factories/specific_attribute_states.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
#
FactoryBot.define do
factory :specific_attribute_state do
checkbox_value { false }
checkbox_value { true }
quantity_box_value { 1 }
room_state { nil }
specific_attribute { nil }

# need to create a virtual attribute becuz room_state and resource need to have the SAME room,
# otherwise validations fail (and becuz of design). we can't use simple 'associations' here
transient do
room { create(:room) }
end
room_state { create(:room_state, room: room) }
specific_attribute { create(:specific_attribute, room: room) }
end
end
42 changes: 41 additions & 1 deletion spec/models/specific_attribute_state_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,45 @@
require 'rails_helper'

RSpec.describe SpecificAttributeState, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
context "the Factory" do
it 'is valid' do
expect(build(:specific_attribute_state)).to be_valid
end
end

context "create specific_attribute_state with all required fields present" do
it 'is valid' do
expect(create(:specific_attribute_state)).to be_valid
end
end

context "create specific_attribute_state without a checkbox_value for specific attribute that needs checkbox" do
it 'raise error "ActiveRecord::RecordInvalid: Validation failed: Checkbox value can\'t be blank if checkbox is required"' do
room = FactoryBot.create(:room)
room_state = FactoryBot.create(:room_state, room: room)
specific_attribute = FactoryBot.create(:specific_attribute, room: room, need_checkbox: true)
state = FactoryBot.build(:specific_attribute_state,
room_state: room_state,
specific_attribute: specific_attribute,
checkbox_value: nil)

expect(state.valid?).to be_falsy
expect(state.errors.full_messages_for(:checkbox_value)).to include "Checkbox value can't be blank if checkbox is required"
end
end

context "create specific_attribute_state without a quantity_box_value for specific attribute that needs quantity box" do
it 'raise error "ActiveRecord::RecordInvalid: Validation failed: Quantity box value can\'t be blank if quantity box is required"' do
room = FactoryBot.create(:room)
room_state = FactoryBot.create(:room_state, room: room)
specific_attribute = FactoryBot.create(:specific_attribute, room: room, need_quantity_box: true)
state = FactoryBot.build(:specific_attribute_state,
room_state: room_state,
specific_attribute: specific_attribute,
quantity_box_value: nil)

expect(state.valid?).to be_falsy
expect(state.errors.full_messages_for(:quantity_box_value)).to include "Quantity box value can't be blank if quantity box is required"
end
end
end

0 comments on commit 0ac931c

Please sign in to comment.