Permalink
Browse files

rails 3 compatibility. still need to implement the controller test

  • Loading branch information...
1 parent 87841b8 commit cf7481eef31d1d7163c24b2292a219cf2d4f40fe @dan-manges committed May 23, 2011
Showing with 35 additions and 18 deletions.
  1. +5 −0 CHANGELOG
  2. +10 −8 README.markdown
  3. +1 −1 Rakefile
  4. +2 −1 lib/unit_record/disconnected_active_record.rb
  5. +3 −1 test/test_helper.rb
  6. +14 −7 test/unit_record/controller_test.rb
View
@@ -1,3 +1,8 @@
+*HEAD*
+
+* Rails 3.0 compatibility
+* Prefer Rails.root to RAILS_ROOT (thanks Arvind Laxminarayan)
+
*0.9.1*
* Rails 2.3 compatibility
View
@@ -12,7 +12,7 @@ One of the biggest benefits to disconnecting unit tests from the database is hav
Finished in 19.302702 seconds.
4920 tests, 7878 assertions, 0 failures, 0 errors
-
+
4 seconds per 1,000 tests is a good guideline.
Installation
@@ -55,7 +55,7 @@ In the <tt>test/unit/unit\_test\_helper.rb</tt> file you created when restructur
require File.dirname(__FILE__) + "/../test_helper"
require "unit_record"
ActiveRecord::Base.disconnect!
-
+
The <tt>disconnect!</tt> method will do everything necessary to run your unit tests without hitting the database.
Strategy
@@ -66,17 +66,17 @@ There are two options for what should happen if you hit the database. You can ei
If you want to raise an exception:
ActiveRecord::Base.disconnect! :strategy => :raise
-
+
Person.find(:all)
#=> RuntimeError: ActiveRecord is disconnected; database access is unavailable in unit tests.
If you want to no-op:
ActiveRecord::Base.disconnect! :strategy => :noop
-
+
Person.find(:all)
#=> []
-
+
You can also change strategies within a block:
ActiveRecord::Base.connection.change_strategy(:raise) do
@@ -88,15 +88,15 @@ You can also change strategies within a block:
Person.find(:all)
#=> []
end
-
+
Association Stubbing
--------------------
One painful aspect of unit testing ActiveRecord classes is setting associations. Because Rails does type checking when setting an association, you'll receive an exception if you try to use a stub in place of the expected class.
Pet.new :owner => stub("person")
#=> ActiveRecord::AssociationTypeMismatch: Person(#16620740) expected, got Mocha::Mock(#11567340)
-
+
If you're using mocha, you can have UnitRecord stub associations. To enable association stubbing:
ActiveRecord::Base.disconnect! :stub_associations => true
@@ -105,7 +105,7 @@ The above example would no longer raise an exception. It would be the equivalent
pet = Pet.new
pet.stubs(:owner).returns(stub("person"))
-
+
Note that using this approach, the setter for the association will not work for that instance.
Development
@@ -129,9 +129,11 @@ Maintainer
Contributors
------------
+* Arvind Laxminarayan
* David Lowenfels
* Rob Sanheim
License
-------
Released under the MIT license
+
View
@@ -59,7 +59,7 @@ task :readme do
sh "open #{file}"
end
-RAILS_VERSIONS = %w[1.2.6 2.0.2 2.1.0 2.1.1 2.2.2 2.3.2]
+RAILS_VERSIONS = %w[1.2.6 2.0.2 2.1.0 2.1.1 2.2.2 2.3.2 3.0.5]
namespace :test do
desc "test with multiple versions of rails"
@@ -14,7 +14,8 @@ def disconnect!(options = {})
UnitRecord.base_rails_test_class.disconnect!
ActiveRecord::Migration.verbose = false
ActiveRecord::Base.connection.change_strategy(:noop) do
- load(RAILS_ROOT + "/db/schema.rb")
+ rails_root = Rails.respond_to?(:root) ? Rails.root : RAILS_ROOT
+ load(rails_root + "/db/schema.rb")
end
end
end
View
@@ -17,7 +17,9 @@
if Rails::VERSION::MAJOR == 2
require "action_controller/test_case"
end
-require "action_controller/test_process"
+if [1, 2].include?(Rails::VERSION::MAJOR)
+ require "action_controller/test_process"
+end
begin
gem "mocha"
@@ -6,26 +6,32 @@ def sample_action
end
end
-ActionController::Routing::Routes.add_route "/sample/sample_action", :controller => "sample", :action => "sample_action"
+case Rails::VERSION::MAJOR
+when 1, 2
+ ActionController::Routing::Routes.add_route "/sample/sample_action", :controller => "sample", :action => "sample_action"
+when 3
+end
-if defined?(ActionController::TestCase) # Rails 2
+if Rails::VERSION::MAJOR == 3
+ # todo
+elsif Rails::VERSION::MAJOR == 2
class ControllerTest < ActionController::TestCase
tests SampleController
-
+
test "render" do
get :sample_action
assert_equal "OK", @response.body
end
-
+
if defined?(ActionController::Caching::SqlCache) # SqlCache goes away in Rails 2.3.1
test "sql caching is enabled" do
assert_equal true, (SampleController < ActionController::Caching::SqlCache)
end
end
end
-else # Rails 1.x
+elsif Rails::VERSION::MAJOR == 1
class ControllerTest < Test::Unit::TestCase
def setup
@@ -38,6 +44,7 @@ def setup
get :sample_action
assert_equal "OK", @response.body
end
- end
-
+ end
+else
+ raise "rails version not tested"
end

0 comments on commit cf7481e

Please sign in to comment.