diff --git a/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb b/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb index 4e2a8b65..a6a8b305 100644 --- a/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb @@ -3,8 +3,20 @@ require 'unit_spec_helper' describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher, type: :model do context 'have_db_index' do it 'accepts an existing index' do - expect(with_index_on(:age1, parent_class: DevelopmentRecord)).to have_db_index(:age1) - expect(with_index_on(:age2, parent_class: ProductionRecord)).to have_db_index(:age2) + model_connected_to_development = with_index_on( + :age1, + model_name: 'EmployeeInDevelopment', + table_name: 'employees_in_development', + parent_class: DevelopmentRecord, + ) + model_connected_to_production = with_index_on( + :age2, + model_name: 'EmployeeInProduction', + table_name: 'employees_in_production', + parent_class: ProductionRecord, + ) + expect(model_connected_to_development).to have_db_index(:age1) + expect(model_connected_to_production).to have_db_index(:age2) end it 'rejects a nonexistent index' do @@ -78,11 +90,15 @@ describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher, type: :model do end def with_index_on(column_name, index_options = {}) + model_name = index_options.delete(:model_name) || 'Employee' + table_name = index_options.delete(:table_name) || 'employees' parent_class = index_options.delete(:parent_class) || ActiveRecord::Base - create_table('employees', connection: parent_class.connection) do |table| + create_table(table_name, connection: parent_class.connection) do |table| table.integer column_name - end.add_index(:employees, column_name, index_options) - define_model_class('Employee', parent_class: parent_class).new + end.add_index(table_name.to_sym, column_name, index_options) + define_model_class(model_name, parent_class: parent_class) do |model| + model.table_name = table_name + end.new end end