Permalink
Browse files

using test_engine to test multiple rails versions

* would like to test different versions of factory girl/devise or something too
  • Loading branch information...
1 parent e55ea78 commit 3284aaae16bdc230d8e5e66dd866574c4b382489 @collin committed Mar 16, 2012
Showing with 233 additions and 139 deletions.
  1. +5 −0 .gitignore
  2. +7 −7 Gemfile
  3. +4 −7 Gemfile.lock
  4. +1 −0 Rakefile
  5. +9 −0 Readme.mdown
  6. +22 −0 test/functional/factory_scenarios_controller_test.rb
  7. +80 −1 test/helper.rb
  8. +1 −124 test/test_factory_scenarios.rb
  9. +104 −0 test/unit/test_scenario.rb
View
@@ -14,6 +14,11 @@ doc
# jeweler generated
pkg
+# test stuff
+test/factory_scenarios.test.yml
+log/
+tmp/
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
#
# * Create a file at ~/.gitignore
View
14 Gemfile
@@ -16,12 +16,12 @@ group :development do
gem "shoulda", ">= 0"
gem "bundler", "~> 1.1.rc8"
gem "jeweler", "~> 1.8.3"
+ gem "test_engine"
+ # platform :ruby_18 do
+ # gem "rcov", ">= 0"
+ # end
- platform :ruby_18 do
- gem "rcov", ">= 0"
- end
-
- platform :ruby_19 do
- gem "simplecov", ">= 0"
- end
+ # platform :ruby_19 do
+ # gem "simplecov", ">= 0"
+ # end
end
View
@@ -76,22 +76,20 @@ GEM
rdoc (~> 3.4)
thor (~> 0.14.6)
rake (0.9.2.2)
- rcov (1.0.0)
rdoc (3.12)
json (~> 1.4)
shoulda (3.0.1)
shoulda-context (~> 1.0.0)
shoulda-matchers (~> 1.0.0)
shoulda-context (1.0.0)
shoulda-matchers (1.0.0)
- simplecov (0.6.1)
- multi_json (~> 1.0)
- simplecov-html (~> 0.5.3)
- simplecov-html (0.5.3)
sprockets (2.1.2)
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
+ test_engine (0.0.4)
+ bundler
+ rails (> 3)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
@@ -112,7 +110,6 @@ DEPENDENCIES
hashie (~> 1.1)
jeweler (~> 1.8.3)
rails (~> 3.0)
- rcov
shoulda
- simplecov
+ test_engine
warden (~> 1.0)
View
@@ -27,6 +27,7 @@ end
Jeweler::RubygemsDotOrgTasks.new
require 'rake/testtask'
+require 'test_engine/test_unit'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/test_*.rb'
View
@@ -12,6 +12,15 @@ Preview email while logged in as users defined in your FactoryGirl factories.
end
* Run bundle install
+
+## Tests
+
+To test with your desired version of rails, do this:
+
+```sh
+RAILS_VERSION=3.1.4 bundel exec rake test
+```
+
## Configuration
* FactoryScenarios is a mountable engine. Add a line like this to your routes.rb file:
@@ -0,0 +1,22 @@
+require 'helper'
+class FactoryScenariosControllerTest < ActionController::TestCase
+ tests FactoryScenarios::ScenariosController
+
+ self.custom_routes = proc do
+ mount FactoryScenarios::Engine => "/factory_scenarios", as: :factory_scenarios
+ end
+
+ def test_iframe_width_assignment
+ User.meta_mock :where, []
+ get(:index, :use_route => :factory_scenarios)
+ assert_equal assigns(:width), "80%"
+ end
+ # context "#index" do
+ # should "assign iframe width" do
+ # end
+ # end
+
+ # context "#enact" do
+
+ # end
+end
View
@@ -9,10 +9,89 @@
end
require 'test/unit'
require 'shoulda'
+require 'rails'
+require 'rails/test_help'
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'factory_scenarios'
-class Test::Unit::TestCase
+# Mocks
+def Object.meta_mock(method, _return)
+ (class << self; self end).send(:define_method, method) {|*args| _return }
end
+
+require "factory_girl"
+require "active_record"
+
+class ::User
+ def save!; true end
+ def id
+ @id ||= rand(10000).to_i
+ end
+end
+
+class ::NotUser; end
+
+FactoryGirl.define do
+ factory :user do; end
+ factory :not_user do; end
+ factory :user_child, :parent => :user do; end
+end
+
+
+# Like with_routing except:
+#
+# * Can be used across an entire TestCase
+# * Can work with a real browser (i.e. Capybara) in integration tests.
+# * Routes are appended to already defined routes
+#
+# Just assign a block to the class attribute "custom_routes". This block will
+# execute as if it is being called from within the routes.rb draw block.
+module TestRoutes
+ extend ActiveSupport::Concern
+
+ included do
+ class_attribute :custom_routes
+ setup :install_routes
+ teardown :restore_routes
+ end
+
+ private
+
+ def install_routes
+ # Easier to refer to
+ routes = Rails.application.routes
+
+ # Clear out any already loaded routes
+ routes.disable_clear_and_finalize = true
+ routes.clear!
+
+ # Load routes from application and engines
+ Rails.application.routes_reloader.paths.each{ |path| load(path) }
+
+ # Bring custom route block into closure and eval in context of draw
+ if custom = self.class.custom_routes
+ puts "mounting routes :D"
+ routes.draw {instance_eval &custom}
+ end
+
+ routes.routes.each do |route|
+ puts route.inspect
+ end
+ ensure
+ # Wrap up route defination
+ routes.disable_clear_and_finalize = false
+ routes.finalize!
+ end
+
+ def restore_routes
+ # Go back to just whatever the app and engines say
+ Rails.application.reload_routes!
+ end
+
+end
+
+class ActionController::TestCase
+ include TestRoutes
+end
@@ -3,27 +3,6 @@
require 'fileutils'
require 'moneta/adapters/yaml'
-# Mocks
-def Object.meta_mock(method, _return)
- (class << self; self end).send(:define_method, method) {|*args| _return }
-end
-
-require "./../factory_scenarios/app/models/factory_scenarios/scenario"
-require "factory_girl"
-require "active_record"
-class ::User
- def save!; true end
- def id
- @id ||= rand(10000).to_i
- end
-end
-class ::NotUser; end
-FactoryGirl.define do
- factory :user do; end
- factory :not_user do; end
- factory :user_child, :parent => :user do; end
-end
-
class TestFactoryScenarios < Test::Unit::TestCase
DEFAULT_FILE_PATH = "/db/factory_scenarios.development.yml"
context "storage" do
@@ -45,106 +24,4 @@ class TestFactoryScenarios < Test::Unit::TestCase
end
end
-
- context FactoryScenarios::Scenario do
- setup do
- @path = FactoryScenarios.config.factory_scenarios_moneta_config[:path] = "./test/factory_scenarios.test.yml"
- @scenario = FactoryScenarios::Scenario.new(FactoryGirl.factories.first)
- end
-
- should "have a storage" do
- assert_equal Moneta::Adapters::YAML, @scenario.storage.class
- end
-
- context "Scenario.all" do
- should "return scenarios only for user factories" do
- assert_equal ["user", "user_child"], FactoryScenarios::Scenario.all.map(&:name)
- end
- end
-
- context "Scenario.find" do
- should "find a scenario by factory name" do
- assert_equal nil, FactoryScenarios::Scenario.find("not_user")
- assert_equal "user", FactoryScenarios::Scenario.find("user").name
- end
- end
-
- context "#enact" do
- context "when persisted? is true" do
- setup do
- @user = User.new
- User.meta_mock(:where, [@user])
- end
-
- should "clear if do_clear is true" do
- @scenario.user_id = 20
- @scenario.enact(true)
- assert_equal 0, @scenario.user_id
- end
-
- should "not clear if do_clear isnt true" do
- @scenario.user_id = 20
- @scenario.enact
- assert_equal 20, @scenario.user_id
- end
- end
-
- should "create a new user if scenario isn't persisted and do_clear is true" do
- User.meta_mock(:where, [])
- assert_not_equal nil, @scenario.enact(true)
- end
- end
-
- context "#find_user" do
- should "return first found user" do
- found = User.new
- User.meta_mock(:where, [found])
- assert_equal found, @scenario.find_user
- end
-
- should "return nothing when no users found" do
- User.meta_mock(:where, [])
- assert_equal nil, @scenario.find_user
- end
- end
-
- should "not be persisted if User.where returns empty array" do
- User.meta_mock :where, []
- assert !@scenario.persisted?
- end
-
- should "be persisted if User.where returns a user" do
- User.meta_mock :where, [User.new]
- assert @scenario.persisted?
- end
-
- should "constantize the configured user class" do
- assert_equal User, @scenario.user_class
- end
-
- should "get its name from it's factory" do
- assert_equal "user", @scenario.name
- end
-
- context "#user_id" do
- should "get its user_id from storage" do
- @scenario.storage["user"] = 50
- assert_equal 50, @scenario.user_id
- end
-
- should "not have a user_id of 0 if storage doesn't have it" do
- @scenario.storage["user"] = 50
- @scenario.storage.delete "user"
- assert_equal 0, @scenario.user_id
- end
- end
-
- context "#clear" do
- should "clear the user_id for a the factory" do
- @scenario.storage["name"] = 500
- @scenario.clear
- assert_equal 0, @scenario.user_id
- end
- end
- end
-end
+end
Oops, something went wrong.

0 comments on commit 3284aaa

Please sign in to comment.