require 'rails_helper' module CustomerVault RSpec.describe PeopleController, :type => :controller do routes { CustomerVault::Engine.routes } describe 'index' do describe 'sorting' do let!(:alice) {FactoryGirl.create(:individual, first_name: 'Alice', last_name: 'Zarston')} let!(:bob) {FactoryGirl.create(:individual, first_name: 'Bob', last_name: 'Tilan')} let!(:corporation) {FactoryGirl.create(:corporation, name: 'Zorg Corp')} it 'should sort people by name by default' do get :index expect(assigns(:people)).to eq([bob, alice, corporation]) end it 'should sort people by name' do get :index, sort: 'name' expect(assigns(:people)).to eq([bob, alice, corporation]) end it 'should sort people by reverse name' do get :index, sort: '-name' expect(assigns(:people)).to eq([corporation, alice, bob]) end context 'sort by zip code' do before :each do alice.address.update_attribute(:zip, 'c123') bob.address.update_attribute(:zip, 'b123') corporation.address.update_attribute(:zip, 'a123') end it 'should sort buildings by zip code' do get :index, sort: 'zip' expect(assigns(:people)).to eq([corporation, bob, alice]) end it 'should sort buildings by reverse zip code' do get :index, sort: '-zip' expect(assigns(:people)).to eq([alice, bob, corporation]) end end end end end end