Skip to content
This repository has been archived by the owner on Jul 24, 2020. It is now read-only.

Commit

Permalink
Refactor EquipmentModels controller spec
Browse files Browse the repository at this point in the history
Resolves #1587
  • Loading branch information
Sydney Young committed Jun 21, 2016
1 parent cc682d5 commit b89ed26
Showing 1 changed file with 52 additions and 103 deletions.
155 changes: 52 additions & 103 deletions spec/controllers/equipment_models_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,104 +1,71 @@
require 'spec_helper'

shared_examples_for 'GET show success' do
shared_examples 'success' do |template|
it { is_expected.to respond_with(:success) }
it { is_expected.to render_template(:show) }
it { is_expected.to render_template(template) }
it { is_expected.not_to set_flash }
end

shared_examples_for 'GET show success' do
it_behaves_like 'success', :show
it 'should set to correct equipment model' do
expect(assigns(:equipment_model)).to eq(model)
end
it 'should set @associated_equipment_models' do
mod1 = FactoryGirl.create(:equipment_model)
model.associated_equipment_models = [mod1]
get :show, id: model
expect(assigns(:associated_equipment_models).size).to eq(1)
expect(assigns(:associated_equipment_models)).to eq([] << mod1)
expect(assigns(:associated_equipment_models)).to eq([mod1])
end

it 'should limit @associated_equipment_models to maximum 6' do
mod1 = FactoryGirl.create(:equipment_model)
mod2 = FactoryGirl.create(:equipment_model)
mod3 = FactoryGirl.create(:equipment_model)
mod4 = FactoryGirl.create(:equipment_model)
mod5 = FactoryGirl.create(:equipment_model)
mod6 = FactoryGirl.create(:equipment_model)
mod7 = FactoryGirl.create(:equipment_model)
model.associated_equipment_models =
[mod1, mod2, mod3, mod4, mod5, mod6, mod7]
FactoryGirl.create_list(:equipment_model, 7)
get :show, id: model
expect(assigns(:associated_equipment_models).size).to eq(6)
end
end

shared_examples_for 'GET index success' do
it { is_expected.to respond_with(:success) }
it { is_expected.to render_template(:index) }
it { is_expected.not_to set_flash }
let!(:other_cat_active) { FactoryGirl.create(:equipment_model) }
let!(:other_cat_inactive) do
FactoryGirl.create(:equipment_model,
deleted_at: Time.zone.today)
end
let!(:same_cat_inactive) do
FactoryGirl.create(:equipment_model, category: model.category,
deleted_at: Time.zone.today)
end
it_behaves_like 'success', :index
context 'without show deleted' do
let!(:mod_other_cat_active) { FactoryGirl.create(:equipment_model) }
let!(:mod_other_cat_inactive) do
FactoryGirl.create(:equipment_model,
deleted_at: Time.zone.today)
end
context 'with @category set' do
it 'should populate an array of of active category-type equipment '\
'models' do
mod_same_cat_inactive =
FactoryGirl.create(:equipment_model, category: model.category,
deleted_at: Time.zone.today)
get :index, category_id: model.category
expect(assigns(:equipment_models).include?(model)).to be_truthy
expect(assigns(:equipment_models).include?(mod_other_cat_active))
.not_to be_truthy
expect(assigns(:equipment_models).include?(mod_same_cat_inactive))
.not_to be_truthy
expect(assigns(:equipment_models).include?(mod_other_cat_inactive))
.not_to be_truthy
expect(assigns(:equipment_models).size).to eq(1)
expect(assigns(:equipment_models)).to eq([model])
end
end
context 'without @category set' do
it 'should populate an array of all active equipment models' do
expect(assigns(:equipment_models).include?(model)).to be_truthy
expect(assigns(:equipment_models).include?(mod_other_cat_active))
.to be_truthy
expect(assigns(:equipment_models).include?(mod_other_cat_inactive))
.not_to be_truthy
expect(assigns(:equipment_models).size).to eq(2)
expect(assigns(:equipment_models)).to \
match_array([model, other_cat_active])
end
end
end
context 'with show deleted' do
let!(:mod_other_cat_active) { FactoryGirl.create(:equipment_model) }
let!(:mod_other_cat_inactive) do
FactoryGirl.create(:equipment_model,
deleted_at: Time.zone.today)
end
context 'with @category set' do
it 'should populate an array of category-type equipment models' do
mod_same_cat_inactive =
FactoryGirl.create(:equipment_model, category: model.category,
deleted_at: Time.zone.today)
get :index, category_id: model.category, show_deleted: true
expect(assigns(:equipment_models).include?(model)).to be_truthy
expect(assigns(:equipment_models).include?(mod_other_cat_active))
.not_to be_truthy
expect(assigns(:equipment_models).include?(mod_same_cat_inactive))
.to be_truthy
expect(assigns(:equipment_models).include?(mod_other_cat_inactive))
.not_to be_truthy
expect(assigns(:equipment_models).size).to eq(2)
expect(assigns(:equipment_models)).to \
match_array([model, same_cat_inactive])
end
end
context 'without @category set' do
it 'should populate an array of all equipment models' do
get :index, show_deleted: true
expect(assigns(:equipment_models).include?(model)).to be_truthy
expect(assigns(:equipment_models).include?(mod_other_cat_active))
.to be_truthy
expect(assigns(:equipment_models).include?(mod_other_cat_inactive))
.to be_truthy
expect(assigns(:equipment_models).size).to eq(3)
expect(assigns(:equipment_models)).to \
match_array([model, other_cat_inactive, other_cat_active,
same_cat_inactive])
end
end
end
Expand Down Expand Up @@ -136,15 +103,7 @@

it_behaves_like 'GET show success'
it 'should include @pending reservations' do
# Make one overdue reservations, one active reservation that started
# in the past, one active reservation starting today,
# one reservation starting within the next week,
# and one starting more than a week in the future
#
#
# First, make sure we have enough equipment objects available
FactoryGirl.create(:equipment_item, equipment_model: model)
FactoryGirl.create(:equipment_item, equipment_model: model)
FactoryGirl.create_pair(:equipment_item, equipment_model: model)
FactoryGirl.create(:missed_reservation, equipment_model: model)
res_starting_today =
FactoryGirl.create(:reservation, equipment_model: model,
Expand All @@ -159,8 +118,8 @@
start_date: Time.zone.today + 10.days,
due_date: Time.zone.today + 12.days)
get :show, id: model
expect(assigns(:pending)).to eq([] << res_starting_today <<
res_starting_this_week)
expect(assigns(:pending)).to eq([res_starting_today,
res_starting_this_week])
end
end

Expand All @@ -169,22 +128,18 @@
sign_in FactoryGirl.create(:user)
get :show, id: model
end

it_behaves_like 'GET show success'
end

context 'availability calendar' do
before(:each) do
@model = FactoryGirl.create(:equipment_model)
2.times do
FactoryGirl.create(:equipment_item, equipment_model: @model)
end
FactoryGirl.create_pair(:equipment_item, equipment_model: model)
sign_in FactoryGirl.create(:user)
end

shared_examples_for 'calculates availability correctly' do |expected|
it do
get :show, id: @model
get :show, id: model
day_data = assigns(:avail_data).select do |d|
d[:start] == Time.zone.today + 1.day
end
Expand All @@ -199,7 +154,7 @@
context 'with upcoming request' do
before do
FactoryGirl.create(:request,
equipment_model: @model,
equipment_model: model,
start_date: Time.zone.today + 1.day,
due_date: Time.zone.today + 1.day)
end
Expand All @@ -210,7 +165,7 @@
context 'with returned reservation' do
before do
FactoryGirl.create(:checked_in_reservation,
equipment_model: @model,
equipment_model: model,
start_date: Time.zone.today - 1.day,
due_date: Time.zone.today + 1.day)
end
Expand All @@ -221,7 +176,7 @@
context 'with upcoming reservation' do
before do
FactoryGirl.create(:valid_reservation,
equipment_model: @model,
equipment_model: model,
start_date: Time.zone.today + 1.day,
due_date: Time.zone.today + 1.day)
end
Expand All @@ -232,12 +187,12 @@
context 'with overdue reservation' do
before do
FactoryGirl.create(:overdue_reservation,
equipment_model: @model,
equipment_model: model,
start_date: Time.zone.today - 1.day,
due_date: Time.zone.today - 1.day,
equipment_item: @model.equipment_items.first)
equipment_item: model.equipment_items.first)
# needed to persist overdue_count
@model.save
model.save
end

it_behaves_like 'calculates availability correctly', 1
Expand All @@ -246,10 +201,10 @@
context 'with checked out reservation' do
before do
FactoryGirl.create(:checked_out_reservation,
equipment_model: @model,
equipment_model: model,
start_date: Time.zone.today - 1.day,
due_date: Time.zone.today + 1.day,
equipment_item: @model.equipment_items.first)
equipment_item: model.equipment_items.first)
end

it_behaves_like 'calculates availability correctly', 1
Expand All @@ -258,16 +213,16 @@
context 'with upcoming and overdue reservations' do
before do
FactoryGirl.create(:valid_reservation,
equipment_model: @model,
equipment_model: model,
start_date: Time.zone.today + 1.day,
due_date: Time.zone.today + 1.day)
FactoryGirl.create(:overdue_reservation,
equipment_model: @model,
equipment_model: model,
start_date: Time.zone.today - 1.day,
due_date: Time.zone.today - 1.day,
equipment_item: @model.equipment_items.first)
equipment_item: model.equipment_items.first)
# needed to persist overdue_count
@model.save
model.save
end

it_behaves_like 'calculates availability correctly', 0
Expand All @@ -281,9 +236,7 @@
sign_in FactoryGirl.create(:admin)
get :new
end
it { is_expected.to respond_with(:success) }
it { is_expected.to render_template(:new) }
it { is_expected.not_to set_flash }
it_behaves_like 'success', :new
it 'assigns a new equipment model to @equipment_model' do
expect(assigns(:equipment_model)).to be_new_record
expect(assigns(:equipment_model)).to be_kind_of(EquipmentModel)
Expand All @@ -292,9 +245,8 @@
expect(assigns(:equipment_model).category).to be_nil
end
it 'sets category when one is passed through params' do
cat = model.category
get :new, category_id: cat
expect(assigns(:equipment_model).category).to eq(cat)
get :new, category_id: model.category
expect(assigns(:equipment_model).category).to eq(model.category)
end
end
context 'with non-admin user' do
Expand All @@ -312,9 +264,7 @@
sign_in FactoryGirl.create(:admin)
get :edit, id: model
end
it { is_expected.to respond_with(:success) }
it { is_expected.to render_template(:edit) }
it { is_expected.not_to set_flash }
it_behaves_like 'success', :edit
it 'sets @equipment_model to selected model' do
expect(assigns(:equipment_model)).to eq(model)
end
Expand All @@ -337,15 +287,15 @@
:equipment_model, category_id: model.category
)
end
it { is_expected.to set_flash }
it { is_expected.to redirect_to(EquipmentModel.last) }
it 'should save model' do
expect do
post :create, equipment_model: FactoryGirl.attributes_for(
:equipment_model, category_id: model.category
)
end.to change(EquipmentModel, :count).by(1)
end.to change { EquipmentModel.count }.by(1)
end
it { is_expected.to set_flash }
it { is_expected.to redirect_to(EquipmentModel.last) }
end

context 'without valid attributes' do
Expand All @@ -363,7 +313,6 @@
name: nil)
end.not_to change(EquipmentModel, :count)
end
it { is_expected.to render_template(:new) }
end
end
context 'with non-admin user' do
Expand All @@ -385,14 +334,14 @@
FactoryGirl.attributes_for(:equipment_model, name: 'Mod')
end
it { is_expected.to set_flash }
it { is_expected.to redirect_to(model) }
it 'sets @equipment_model to selected model' do
expect(assigns(:equipment_model)).to eq(model)
end
it 'updates attributes' do
model.reload
expect(model.name).to eq('Mod')
end
it { is_expected.to redirect_to(model) }
end

context 'without valid attributes' do
Expand All @@ -401,11 +350,11 @@
FactoryGirl.attributes_for(:equipment_model, name: nil)
end
it { is_expected.not_to set_flash }
it { is_expected.to render_template(:edit) }
it 'should not update attributes' do
model.reload
expect(model.name).not_to be_nil
end
it { is_expected.to render_template(:edit) }
end
it 'calls delete_files'
end
Expand Down

0 comments on commit b89ed26

Please sign in to comment.