mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
adding tests for foreign keys
git-svn-id: https://svn.thoughtbot.com/plugins/shoulda/trunk@259 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
This commit is contained in:
parent
fb26b71fdf
commit
5b90600cd8
9 changed files with 47 additions and 4 deletions
|
@ -281,15 +281,26 @@ module ThoughtBot # :nodoc:
|
|||
through = get_options!(associations, :through)
|
||||
klass = model_class
|
||||
associations.each do |association|
|
||||
should "have many #{association}#{" through #{through}" if through}" do
|
||||
name = "have many #{association}"
|
||||
name += " through #{through}" if through
|
||||
should name do
|
||||
reflection = klass.reflect_on_association(association)
|
||||
assert reflection, "#{klass.name} does not have any relationship to #{association}"
|
||||
assert_equal :has_many, reflection.macro
|
||||
|
||||
if through
|
||||
through_reflection = klass.reflect_on_association(through)
|
||||
assert through_reflection, "#{klass.name} does not have any relationship to #{through}"
|
||||
assert_equal(through, reflection.options[:through])
|
||||
end
|
||||
|
||||
unless reflection.options[:through]
|
||||
# This is not a through association, so check for the existence of the foreign key on the other table
|
||||
fk = reflection.options[:foreign_key] || "#{klass.name.downcase}_id"
|
||||
associated_klass = reflection.options[:class_name] || association.to_s.classify
|
||||
associated_klass = associated_klass.constantize
|
||||
assert associated_klass.column_names.include?(fk.to_s), "#{associated_klass.name} does not have a #{fk} foreign key."
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
8
test/README
Normal file
8
test/README
Normal file
|
@ -0,0 +1,8 @@
|
|||
The tests for should have two dependencies that I know of:
|
||||
|
||||
* Rails version 1.2.3
|
||||
* A working sqlite3 installation.
|
||||
|
||||
If you have problems running these tests, please notify the shoulda mailing list: shoulda@googlegroups.com
|
||||
|
||||
- Tammer Saleh
|
|
@ -49,7 +49,7 @@ class PostsController < ApplicationController
|
|||
respond_to do |format|
|
||||
if @post.update_attributes(params[:post])
|
||||
flash[:notice] = 'Post was successfully updated.'
|
||||
format.html { redirect_to post_url(@post) }
|
||||
format.html { redirect_to post_url(@post.user, @post) }
|
||||
format.xml { head :ok }
|
||||
else
|
||||
format.html { render :action => "edit" }
|
||||
|
@ -65,7 +65,7 @@ class PostsController < ApplicationController
|
|||
flash[:notice] = "Post was removed"
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to posts_url }
|
||||
format.html { redirect_to posts_url(@post.user) }
|
||||
format.xml { head :ok }
|
||||
end
|
||||
end
|
||||
|
|
3
test/rails_root/app/models/dog.rb
Normal file
3
test/rails_root/app/models/dog.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Dog < ActiveRecord::Base
|
||||
belongs_to :user, :foreign_key => :owner_id
|
||||
end
|
|
@ -1,5 +1,6 @@
|
|||
class User < ActiveRecord::Base
|
||||
has_many :posts
|
||||
has_many :dogs, :foreign_key => :owner_id
|
||||
|
||||
attr_protected :password
|
||||
validates_format_of :email, :with => /\w*@\w*.com/
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
# Specifies gem version of Rails to use when vendor/rails is not present
|
||||
#RAILS_GEM_VERSION = '1.1.6'
|
||||
old_verbose, $VERBOSE = $VERBOSE, nil
|
||||
RAILS_GEM_VERSION = '1.2.3'
|
||||
$VERBOSE = old_verbose
|
||||
|
||||
require File.join(File.dirname(__FILE__), 'boot')
|
||||
|
||||
|
|
11
test/rails_root/db/migrate/005_create_dogs.rb
Normal file
11
test/rails_root/db/migrate/005_create_dogs.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
class CreateDogs < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :dogs do |t|
|
||||
t.column :owner_id, :integer
|
||||
end
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :dogs
|
||||
end
|
||||
end
|
6
test/unit/dog_test.rb
Normal file
6
test/unit/dog_test.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
|
||||
class DogTest < Test::Unit::TestCase
|
||||
load_all_fixtures
|
||||
should_belong_to :user
|
||||
end
|
|
@ -4,6 +4,7 @@ class UserTest < Test::Unit::TestCase
|
|||
load_all_fixtures
|
||||
|
||||
should_have_many :posts
|
||||
should_have_many :dogs
|
||||
|
||||
should_not_allow_values_for :email, "blah", "b lah"
|
||||
should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
|
||||
|
|
Loading…
Add table
Reference in a new issue