diff --git a/spec/unit/shoulda/matchers/active_record/have_implicit_order_column_spec.rb b/spec/unit/shoulda/matchers/active_record/have_implicit_order_column_spec.rb index 5fe82fcf6..d00f74fa2 100644 --- a/spec/unit/shoulda/matchers/active_record/have_implicit_order_column_spec.rb +++ b/spec/unit/shoulda/matchers/active_record/have_implicit_order_column_spec.rb @@ -3,114 +3,146 @@ describe Shoulda::Matchers::ActiveRecord::HaveImplicitOrderColumnMatcher, type: :model do if active_record_supports_implicit_order_column? context 'when implicit_order_column is defined for the column' do - let(:defined_model) { model(created_at: :timestamp) } - - before do - defined_model.class.implicit_order_column = 'created_at' - end - context 'when column name is a symbol' do it 'accepts' do - expect(defined_model).to have_implicit_order_column(:created_at) + record = record_with_implicit_order_column_on( + 'created_at', + columns: { created_at: :timestamp }, + ) + + expect(record).to have_implicit_order_column(:created_at) end end context 'when column name is a string' do it 'accepts' do - expect(defined_model).to have_implicit_order_column('created_at') + record = record_with_implicit_order_column_on( + 'created_at', + columns: { created_at: :timestamp }, + ) + + expect(record).to have_implicit_order_column('created_at') end end end context 'when implicit_order_column is defined for another column' do - let(:defined_model) { model(created_at: :timestamp, email: :string) } - let(:message) do - format_message(<<-MESSAGE, one_line: true) - Expected Employee to have implicit_order_column set to email - (Employee implicit_order_column is set to created_at) - MESSAGE - end - - before do - defined_model.class.implicit_order_column = 'created_at' - end - context 'when column name is a symbol' do it 'rejects with an appropriate failure message' do + record = record_with_implicit_order_column_on( + 'created_at', + columns: { created_at: :timestamp, email: :string }, + ) + assertion = lambda { - expect(defined_model).to have_implicit_order_column(:email) + expect(record).to have_implicit_order_column(:email) } + message = format_message(<<-MESSAGE, one_line: true) + Expected Employee to have implicit_order_column set to email + (Employee implicit_order_column is set to created_at) + MESSAGE + expect(&assertion).to fail_with_message(message) end end context 'when column name is a string' do it 'rejects with an appropriate failure message' do + record = record_with_implicit_order_column_on( + 'created_at', + columns: { created_at: :timestamp, email: :string }, + ) + assertion = lambda { - expect(defined_model).to have_implicit_order_column('email') + expect(record).to have_implicit_order_column('email') } + message = format_message(<<-MESSAGE, one_line: true) + Expected Employee to have implicit_order_column set to email + (Employee implicit_order_column is set to created_at) + MESSAGE + expect(&assertion).to fail_with_message(message) end end end context 'when implicit_order_column is NOT defined on model' do - let(:defined_model) { model(created_at: :timestamp) } - let(:message) do - format_message(<<-MESSAGE, one_line: true) - Expected Employee to have implicit_order_column set to created_at - (Employee implicit_order_column is not set) - MESSAGE - end - context 'when column name is a symbol' do it 'rejects with an appropriate failure message' do + record = record_without_implicit_order_column( + columns: { created_at: :timestamp }, + ) + assertion = lambda { - expect(defined_model).to have_implicit_order_column(:created_at) + expect(record).to have_implicit_order_column(:created_at) } + message = format_message(<<-MESSAGE, one_line: true) + Expected Employee to have implicit_order_column set to created_at + (Employee implicit_order_column is not set) + MESSAGE + expect(&assertion).to fail_with_message(message) end end context 'when column name is a string' do it 'rejects with an appropriate failure message' do + record = record_without_implicit_order_column( + columns: { created_at: :timestamp }, + ) + assertion = lambda { - expect(defined_model).to have_implicit_order_column('created_at') + expect(record).to have_implicit_order_column('created_at') } + message = format_message(<<-MESSAGE, one_line: true) + Expected Employee to have implicit_order_column set to created_at + (Employee implicit_order_column is not set) + MESSAGE + expect(&assertion).to fail_with_message(message) end end end context 'when given column does NOT exist' do - let(:defined_model) { model(created_at: :timestamp) } - let(:message) do - format_message(<<-MESSAGE, one_line: true) - Expected Employee to have implicit_order_column set to whatever - (Employee does not have a db column named whatever) - MESSAGE - end - context 'when column name is a symbol' do it 'rejects with an appropriate failure message' do + record = record_without_implicit_order_column( + columns: { created_at: :timestamp }, + ) + assertion = lambda { - expect(defined_model).to have_implicit_order_column(:whatever) + expect(record).to have_implicit_order_column(:whatever) } + message = format_message(<<-MESSAGE, one_line: true) + Expected Employee to have implicit_order_column set to whatever + (Employee does not have a db column named whatever) + MESSAGE + expect(&assertion).to fail_with_message(message) end end context 'when column name is a string' do it 'rejects with an appropriate failure message' do + record = record_without_implicit_order_column( + columns: { created_at: :timestamp }, + ) + assertion = lambda { - expect(defined_model).to have_implicit_order_column('whatever') + expect(record).to have_implicit_order_column('whatever') } + message = format_message(<<-MESSAGE, one_line: true) + Expected Employee to have implicit_order_column set to whatever + (Employee does not have a db column named whatever) + MESSAGE + expect(&assertion).to fail_with_message(message) end end @@ -125,8 +157,14 @@ end end - def model(options = {}) - define_model(:employee, options).new + def record_with_implicit_order_column_on(column_name, columns:) + define_model(:employee, columns) do |model| + model.implicit_order_column = column_name + end.new + end + + def record_without_implicit_order_column(columns:) + define_model(:employee, columns).new end end end