Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor have_db_index_matcher#correct_unique?

Currently, there is an assumption that `matched_index.unique` will
be `true` and not truthy. This is not always the case. This can
cause tests to fail, even though they should pass. This allows
`matched_index.unique` to be truthy.
  • Loading branch information...
commit 91e98a7aa0c80685e6974d86f232be5d2c97cf4b 1 parent 27841b7
@steveklabnik authored
View
13 lib/shoulda/matchers/active_record/have_db_index_matcher.rb
@@ -62,13 +62,16 @@ def index_exists?
def correct_unique?
return true unless @options.key?(:unique)
- if matched_index.unique == @options[:unique]
- true
- else
+ is_unique = matched_index.unique
+
+ is_unique = !is_unique unless @options[:unique]
+
+ unless is_unique
@missing = "#{table_name} has an index named #{matched_index.name} " <<
- "of unique #{matched_index.unique}, not #{@options[:unique]}."
- false
+ "of unique #{matched_index.unique}, not #{@options[:unique]}."
end
+
+ is_unique
end
def matched_index
View
17 spec/shoulda/active_record/have_db_index_matcher_spec.rb
@@ -85,4 +85,21 @@
it "should not context an index's uniqueness when it isn't important" do
have_db_index(:user_id).description.should_not =~ /unique/
end
+
+ it "allows an IndexDefinition to have a truthy value for unique" do
+ db_connection = create_table 'superheros' do |table|
+ table.integer :age
+ end
+ db_connection.add_index :superheros, :age
+ define_model_class 'Superhero'
+
+ @matcher = have_db_index(:age).unique(true)
+
+ index_definition = stub("ActiveRecord::ConnectionAdapters::IndexDefinition",
+ :unique => 7,
+ :name => :age)
+ @matcher.stubs(:matched_index => index_definition)
+
+ Superhero.new.should @matcher
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.