public
Fork of thoughtbot/shoulda
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/technicalpickles/shoulda.git
Search Repo:
- removed a bunch of unneeded files under the test/rails_root
- added tests for should_have_and_belong_to_many
- updated the testing readme to explain the testing setup
- documentation



git-svn-id: https://svn.thoughtbot.com/plugins/shoulda/trunk@446 
7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
tsaleh (author)
Tue Apr 08 05:53:25 -0700 2008
commit  452b49c5f7e37906f1e73ab462605021ad123d97
tree    c3df06781f09c417a278041ff115b376a8f403de
parent  fabe6b7724b5fad0f86c6503ed5801de6ed3020f
  • lib/shoulda/active_record_helpers.rb
  • lib/shoulda/private_helpers.rb
  • test/README
  • test/rails_root/Rakefile
  • test/rails_root/app/models/dog.rb
  • test/rails_root/app/models/flea.rb
  • test/rails_root/db/migrate/007_create_fleas.rb
  • test/rails_root/doc/README_FOR_APP
  • test/rails_root/public/dispatch.cgi
  • test/rails_root/public/dispatch.fcgi
  • test/rails_root/public/dispatch.rb
  • test/rails_root/public/favicon.ico
  • test/rails_root/public/images/rails.png
  • test/rails_root/public/index.html
  • test/rails_root/public/javascripts/application.js
  • test/rails_root/public/javascripts/controls.js
  • test/rails_root/public/javascripts/dragdrop.js
  • test/rails_root/public/javascripts/effects.js
  • test/rails_root/public/javascripts/prototype.js
  • test/rails_root/public/robots.txt
  • test/rails_root/public/stylesheets/scaffold.css
  • test/rails_root/script/about
  • test/rails_root/script/breakpointer
  • test/rails_root/script/destroy
  • test/rails_root/script/performance/benchmarker
  • test/rails_root/script/performance/profiler
  • test/rails_root/script/plugin
  • test/rails_root/script/process/inspector
  • test/rails_root/script/process/reaper
  • test/rails_root/script/process/spawner
  • test/rails_root/script/runner
  • test/rails_root/script/server
  • test/unit/dog_test.rb
  • test/unit/flea_test.rb
...
77
78
79
 
 
 
 
 
80
81
82
 
83
84
85
...
90
91
92
93
94
 
 
95
96
97
...
102
103
104
105
106
107
 
 
 
108
109
110
...
381
382
383
 
 
 
384
385
386
387
388
389
390
391
392
 
 
 
 
393
394
395
...
77
78
79
80
81
82
83
84
85
 
 
86
87
88
89
...
94
95
96
 
 
97
98
99
100
101
...
106
107
108
 
 
 
109
110
111
112
113
114
...
385
386
387
388
389
390
391
392
 
393
394
395
396
 
 
397
398
399
400
401
402
403
0
@@ -77,9 +77,13 @@ module ThoughtBot # :nodoc:
0
             
0
             assert_contains(object.errors.on(attribute), message)
0
             
0
+ # Now test that the object is valid when changing the scoped attribute
0
+ # TODO: There is a chance that we could change the scoped field
0
+ # to a value that's already taken. An alternative implementation
0
+ # could actually find all values for scope and create a unique
0
+ # one.
0
             if scope
0
- # Now test that the object is valid when changing the scoped attribute
0
- # TODO: actually find all values for scope and create a unique one.
0
+ # Assume the scope is a foreign key if the field is nil
0
               object.send(:"#{scope}=", existing.send(scope).nil? ? 1 : existing.send(scope).next)
0
               object.errors.clear
0
               object.valid?
0
@@ -90,8 +94,8 @@ module ThoughtBot # :nodoc:
0
         end
0
       end
0
 
0
- # Ensures that the attribute cannot be set on update
0
- # Requires an existing record
0
+ # Ensures that the attribute cannot be set on mass update.
0
+ # Requires an existing record.
0
       #
0
       # should_protect_attributes :password, :admin_flag
0
       #
0
@@ -102,9 +106,9 @@ module ThoughtBot # :nodoc:
0
         attributes.each do |attribute|
0
           attribute = attribute.to_sym
0
           should "protect #{attribute} from mass updates" do
0
- protected = klass.protected_attributes
0
- assert protected.include?(attribute.to_s),
0
- "#{klass} is protecting #{protected.to_a.to_sentence}, but not #{attribute}."
0
+ protected_attributes = klass.protected_attributes
0
+ assert protected_attributes.include?(attribute.to_s),
0
+ "#{klass} is protecting #{protected_attributes.to_a.to_sentence}, but not #{attribute}."
0
           end
0
         end
0
       end
0
@@ -381,15 +385,19 @@ module ThoughtBot # :nodoc:
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
-
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), "#{klass.name} does not have any relationship to #{association}"
0
- assert_equal :has_and_belongs_to_many, klass.reflect_on_association(association).macro
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
           end
0
         end
0
       end
...
1
2
3
 
 
4
5
6
...
9
10
11
 
 
 
12
13
14
...
1
2
3
4
5
6
7
8
...
11
12
13
14
15
16
17
18
19
0
@@ -1,6 +1,8 @@
0
 module ThoughtBot # :nodoc:
0
   module Shoulda # :nodoc:
0
     module Private # :nodoc:
0
+ # Returns the values for the entries in the args hash who's keys are listed in the wanted array.
0
+ # Will raise if there are keys in the args hash that aren't listed.
0
       def get_options!(args, *wanted)
0
         ret = []
0
         opts = (args.last.is_a?(Hash) ? args.pop : {})
0
@@ -9,6 +11,9 @@ module ThoughtBot # :nodoc:
0
         return *ret
0
       end
0
 
0
+ # Returns the model class constant, as determined by the test class name.
0
+ #
0
+ # class TestUser; model_class; end => User
0
       def model_class
0
         self.name.gsub(/Test$/, '').constantize
0
       end
...
1
 
2
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
5
6
 
7
8
9
 
...
 
1
2
 
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
34
35
 
36
37
0
@@ -1,8 +1,36 @@
0
-The tests for should have two dependencies that I know of:
0
+The Shoulda test suite (in particular - the tests that test shoulda)
0
 
0
-* Rails version 1.2.3
0
+Quick overview:
0
+
0
+The test directory contains the following files and subdirectories:
0
+
0
+* rails_root - contains the stripped down rails application that the tests run against. The rails root contains:
0
+** the models, controllers, and views defined under app/
0
+** the sqlite3.rb environment file
0
+** a migration file for each model
0
+* fixtures - contain the sample DB data for each model
0
+* functional - controller tests for each of the controllers under rails_root/app
0
+* unit - model tests for each of the models under rails_root/app
0
+* other - tests for the shoulda contexts, should statements, and assertions
0
+* test_helper.rb - responsible for initializing the test environment
0
+** sets the rails_env to sqlite3
0
+** sets the rails_root
0
+** creates the rails_root/vendor/plugins/shoulda symlink
0
+** runs all the migrations against the in-memory sqlite3 db
0
+** adds some magic to load the right fixture files
0
+
0
+In order to add a new model (or controller) to the test suite:
0
+
0
+* add that model to rails_root/app/models
0
+* add a migration for that model
0
+* add a fixture file
0
+* add a test for that file under test/units
0
+
0
+Dependencies:
0
+
0
+* Rails gem installed in the host system
0
 * A working sqlite3 installation.
0
 
0
-If you have problems running these tests, please notify the shoulda mailing list: shoulda@googlegroups.com
0
+If you have problems running these tests, please notify the mailing list: shoulda@googlegroups.com
0
 
0
-- Tammer Saleh
0
\ No newline at end of file
0
+- Tammer Saleh <tsaleh@thoughtbot.com>
...
1
2
 
3
...
1
2
3
4
0
@@ -1,3 +1,4 @@
0
 class Dog < ActiveRecord::Base
0
   belongs_to :user, :foreign_key => :owner_id
0
+ has_and_belongs_to_many :fleas
0
 end
...
3
4
5
 
6
...
3
4
5
6
7
0
@@ -3,4 +3,5 @@ require File.dirname(__FILE__) + '/../test_helper'
0
 class DogTest < Test::Unit::TestCase
0
   load_all_fixtures
0
   should_belong_to :user
0
+ should_have_and_belong_to_many :fleas
0
 end

Comments

    No one has commented yet.