public
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/thoughtbot/shoulda.git
Search Repo:
fixed should_have_and_belong_to to test for the join table
tsaleh (author)
Sat Apr 26 14:42:14 -0700 2008
commit  28d9140a7ea45f9a305b251a544ead425fcd58e4
tree    f2dd24073247c9ccd8572e4d778c39247bcdad55
parent  102fe37adc9a9b2c23132791bf16c0ff13553b3b
...
415
416
417
418
 
 
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
 
 
 
 
 
435
436
437
...
415
416
417
 
418
419
420
421
422
 
 
 
423
424
425
426
427
428
 
 
 
 
429
430
431
432
433
434
435
436
0
@@ -415,23 +415,22 @@ module ThoughtBot # :nodoc:
0
         end
0
       end
0
   
0
- # Ensures that the has_and_belongs_to_many relationship exists.
0
+ # Ensures that the has_and_belongs_to_many relationship exists, and that the join
0
+ # table is in place.
0
       #
0
       # should_have_and_belong_to_many :posts, :cars
0
       #
0
- # NOTE: One thing this macro should test, but doesn't is that the join
0
- # table exists in the DB. Please contact the author if you know of a DB
0
- # agnostic way of introspecting on the current schema.
0
       def should_have_and_belong_to_many(*associations)
0
         get_options!(associations)
0
         klass = model_class
0
 
0
         associations.each do |association|
0
           should "should have and belong to many #{association}" do
0
- assert klass.reflect_on_association(association),
0
- "#{klass.name} does not have any relationship to #{association}"
0
- assert_equal :has_and_belongs_to_many,
0
- klass.reflect_on_association(association).macro
0
+ reflection = klass.reflect_on_association(association)
0
+ assert reflection, "#{klass.name} does not have any relationship to #{association}"
0
+ assert_equal :has_and_belongs_to_many, reflection.macro
0
+ table = reflection.options[:join_table]
0
+ assert ::ActiveRecord::Base.connection.tables.include?(table), "table #{table} doesn't exist"
0
           end
0
         end
0
       end
...
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
0
@@ -0,0 +1,12 @@
0
+class CreateDogsFleas < ActiveRecord::Migration
0
+ def self.up
0
+ create_table :dogs_fleas do |t|
0
+ t.integer :dog_id
0
+ t.integer :flea_id
0
+ end
0
+ end
0
+
0
+ def self.down
0
+ drop_table :dogs_fleas
0
+ end
0
+end

Comments

    No one has commented yet.