1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00

fixed should_have_and_belong_to to test for the join table

git-svn-id: https://svn.thoughtbot.com/plugins/shoulda/trunk@488 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
This commit is contained in:
tsaleh 2008-05-01 13:49:38 +00:00
parent 0cd585928f
commit fa37ef07af
2 changed files with 19 additions and 8 deletions

View file

@ -415,23 +415,22 @@ module ThoughtBot # :nodoc:
end
end
# Ensures that the has_and_belongs_to_many relationship exists.
# Ensures that the has_and_belongs_to_many relationship exists, and that the join
# table is in place.
#
# should_have_and_belong_to_many :posts, :cars
#
# NOTE: One thing this macro should test, but doesn't is that the join
# table exists in the DB. Please contact the author if you know of a DB
# agnostic way of introspecting on the current schema.
def should_have_and_belong_to_many(*associations)
get_options!(associations)
klass = model_class
associations.each do |association|
should "should have and belong to many #{association}" do
assert klass.reflect_on_association(association),
"#{klass.name} does not have any relationship to #{association}"
assert_equal :has_and_belongs_to_many,
klass.reflect_on_association(association).macro
reflection = klass.reflect_on_association(association)
assert reflection, "#{klass.name} does not have any relationship to #{association}"
assert_equal :has_and_belongs_to_many, reflection.macro
table = reflection.options[:join_table]
assert ::ActiveRecord::Base.connection.tables.include?(table), "table #{table} doesn't exist"
end
end
end

View file

@ -0,0 +1,12 @@
class CreateDogsFleas < ActiveRecord::Migration
def self.up
create_table :dogs_fleas do |t|
t.integer :dog_id
t.integer :flea_id
end
end
def self.down
drop_table :dogs_fleas
end
end