Skip to content

Commit

Permalink
Major new direction and testing setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
metaskills committed Jan 15, 2013
1 parent 364c113 commit 6d5d3ae
Show file tree
Hide file tree
Showing 41 changed files with 469 additions and 112 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.bundle
Gemfile.lock
pkg/*
*.log
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: ruby
rvm:
- ree
- 1.8.7
- 1.9.2
- 1.9.3
- jruby-18mode
- jruby-19mode
- rbx-18mode
- rbx-19mode
gemfile:
- gemfiles/rails3.0.x.gemfile
- gemfiles/rails3.1.x.gemfile
- gemfiles/rails3.2.x.gemfile
- gemfiles/rails4.0.x.gemfile
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

## v4.3.0

* Track MiniTest's major/minior version number.
* Remove alternate MiniTest::Spec Describe support.


## v3.0.7

* Must use MiniTest version ~> 2.1. As 3.x will not work.
Expand Down
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
source :rubygems
gemspec
eval File.read('gemfiles/rails3.2.x.gemfile')
55 changes: 18 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,46 @@
# MiniTest::Spec For Rails

This gem makes it easy to use the MiniTest Spec DSL in Rails 3 application tests.
This gem makes it easy to use the MiniTest Spec DSL within your existing Rails 3 or 4 application tests.

# Advantages

## Advantages

With minitest-spec-rails, we have a working solution by replacing MiniTest::Spec as the superclass for ActiveSupport::TestCase. This solution is simple and does not require you to recreate a new test case in your test_helper.rb or to use generators supplied by gems like minitest-rails.

MinitTest::Spec is built on top of MiniTest::Unit which a replacement for Test::Unit, so is stable and consistent. It's easy to change an existing Rails application from Test Unit to MiniTest::Spec simply installing this gem. As Rails evolves to leverage MiniTest::Spec, your test case code will not have to change at both the class definition and internals.
MinitTest::Spec is built on top of MiniTest::Unit, a replacement for Test::Unit, so it is stable and consistent. It's easy to change an existing Rails application from Test::Unit/MiniTest::Unit to MiniTest::Spec by simply installing this gem. As Rails evolves to leverage MiniTest::Spec [(if ever)](http:https://github.com/rails/rails/commit/b22c527e65a41da59dbfcb078968069c6fae5086), your test case code will not have to change at both the class definition and internals.


# How it works
## How it works

This gem makes Test::Unit::TestCase a subclass of of MiniTest::Spec with a simple shim. The gem tricks ActiveSupport::TestCase to use MiniTest::Spec instead. The gem includes any monkey patches to make using MiniTest::Spec a drop in behavior change for any Rails 3.x project.

Full Details here:

http:https://metaskills.net/2011/03/26/using-minitest-spec-with-rails/

# Styles
## Styles

This <a href="http:https://cheat.errtheblog.com/s/minitest/1">cheat sheet</a> shows the Test::Unit assert methods and the MiniTest::Spec methods. Remember, MiniTest::Spec is build on top of MiniTest::Unit which is a Test::Unit replacement. That means you can mix and match styles as you upgrade from Test::Unit to a more modern style.

Test::Unit Style:

assert_equal 100, foo
```ruby
assert_equal 100, foo
```

MiniTest::Spec Style:

foo.must_equal 100
```ruby
foo.must_equal 100
```

There are a few missing assertions available in Test::Unit that are changed or no longer available in MiniTest.

* The method <code>assert_raise</code> is renamed <code>assert_raises</code>.
* There is no method <code>assert_nothing_raised</code>. There are good reasons for this on Ryan's blog.

# Alternate MiniTest::Spec Describe

It is also possible to change the style of your test files and their super classes. In these examples below, the gem will take care of setting your super class and proper setups.

# ./test/unit/post_test.rb
require 'test_helper'
describe Post do
it "must be valid" do
@post.must_be :valid?
end
end

# ./test/functional/posts_controller_test.rb
require 'test_helper'
describe PostController do
describe "on GET to :show" do
before do
get :show, id: "1"
end
it "returns a post object " do
post = assigns(:post)
post.wont_be_nil
end
end
end

# Issues
* The method `assert_raise` is renamed `assert_raises`.
* There is no method `assert_nothing_raised`. There are good reasons for this on [Ryan's blog entry](http:https://blog.zenspider.com/blog/2012/01/assert_nothing_tested.html).


## Issues

If there are any issues using this in Rails 3 outside the normal few assertions that change from Test::Unit to MiniTest::Unit, please open an issue here on github.

Expand Down
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ Rake::TestTask.new do |t|
end

task :default => [:test]

4 changes: 4 additions & 0 deletions gemfiles/common.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source :rubygems

gemspec :path => File.expand_path('../',__FILE__)

3 changes: 3 additions & 0 deletions gemfiles/rails3.0.x.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eval File.read('gemfiles/common.gemfile')

gem 'rails', '~> 3.0.0'
3 changes: 3 additions & 0 deletions gemfiles/rails3.1.x.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eval File.read('gemfiles/common.gemfile')

gem 'rails', '~> 3.1.0'
4 changes: 4 additions & 0 deletions gemfiles/rails3.2.x.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eval File.read('gemfiles/common.gemfile')

gem 'rails', '~> 3.2.0'

Empty file added gemfiles/rails4.0.x.gemfile
Empty file.
9 changes: 7 additions & 2 deletions lib/minitest-spec-rails.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
module MiniTestSpecRails
end

require 'rails'
require 'minitest/spec'
require 'test/unit/testcase'
require 'minitest-spec-rails/version'
require 'minitest-spec-rails/test_case'
require 'minitest-spec-rails/railtie'
require 'test/unit/testcase'

30 changes: 30 additions & 0 deletions lib/minitest-spec-rails/init/action_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ActionController::TestCase.instance_eval do

register_spec_type(self) do |desc|
Class === desc && desc < ActionController::Metal
end

register_spec_type(/Controller( ?Test)?\z/i, self)

end

ActionDispatch::IntegrationTest.instance_eval do

register_spec_type(/(Acceptance|Integration) ?Test\z/i, self)

end

# class ActionController::TestCase
# def self.determine_default_controller_class(name)
# if name.match(/.*(?:^|::)(\w+Controller)/)
# $1.constantize rescue nil
# else
# super(name)
# end
# end
# before { setup_controller_request_and_response }
# end

# class ActionController::IntegrationTest
# before { self.class.app = Rails.application }
# end
9 changes: 9 additions & 0 deletions lib/minitest-spec-rails/init/action_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ActionMailer::TestCase.instance_eval do

register_spec_type(self) do |desc|
Class === desc && desc < ActionMailer::Base
end

register_spec_type(/Mailer( ?Test)?\z/i, self)

end
5 changes: 5 additions & 0 deletions lib/minitest-spec-rails/init/action_view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ActionView::TestCase.instance_eval do

register_spec_type(/(Helper|View)( ?Test)?\z/i, self)

end
11 changes: 11 additions & 0 deletions lib/minitest-spec-rails/init/active_support.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ActiveSupport::TestCase.instance_eval do

class << self
remove_method :describe
end

register_spec_type(self) do |desc|
Class === desc && desc < ActiveRecord::Base
end

end
17 changes: 17 additions & 0 deletions lib/minitest-spec-rails/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module MiniTestSpecRails
class Railtie < ::Rails::Railtie

config.minitest_spec_rails = ActiveSupport::OrderedOptions.new

config.before_initialize do |app|
ActiveSupport.on_load(:action_view) { require 'minitest-spec-rails/init/action_view' }
ActiveSupport.on_load(:action_controller) { require 'minitest-spec-rails/init/action_controller' }
ActiveSupport.on_load(:action_mailer) { require 'minitest-spec-rails/init/action_mailer' }
end

initializer 'minitest-spec-rails.after.load_active_support', :after => :load_active_support, :group => :all do |app|
require 'minitest-spec-rails/init/active_support'
end

end
end
33 changes: 0 additions & 33 deletions lib/minitest-spec-rails/test_case.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/minitest-spec-rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module MiniTestSpecRails
VERSION = "3.0.7"
VERSION = "4.3.0"
end
6 changes: 1 addition & 5 deletions lib/test/unit.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
require 'minitest/autorun'
require 'test/unit/assertions'
require 'test/unit/testcase'

module Test
module Unit

TEST_UNIT_IMPLEMENTATION = 'test/unit mock using minitest-spec-rails'
end
end
2 changes: 0 additions & 2 deletions lib/test/unit/assertions.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'minitest/spec'

module Test
module Unit
Assertions = ::MiniTest::Assertions
Expand Down
18 changes: 5 additions & 13 deletions lib/test/unit/testcase.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
require 'minitest/spec'
require 'test/unit/assertions'
require 'pp'
require 'minitest-spec-rails'
require 'minitest/autorun'

module Test
module Unit

remove_const(:TestCase) if defined?(self::TestCase)
class TestCase < ::MiniTest::Spec

MINI_SPECUNIT = true

def mu_pp(obj)
obj.pretty_inspect.chomp
end

def build_message(head, template=nil, *arguments)
template &&= template.chomp
template.gsub(/\?/) { mu_pp(arguments.shift) }
end
MINITEST_SPEC_RAILS = true

end

Expand Down
35 changes: 19 additions & 16 deletions minitest-spec-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
$:.push File.expand_path("../lib", __FILE__)
require "minitest-spec-rails/version"

Gem::Specification.new do |s|
s.name = 'minitest-spec-rails'
s.version = MiniTestSpecRails::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ['Ken Collins']
s.email = ['[email protected]']
s.homepage = 'http:https://github.com/metaskills/minitest-spec-rails'
s.summary = 'Drop in MiniTest::Spec support for Rails 3.x'
s.description = 'Force ActiveSupport::TestCase to subclass MiniTest::Spec for a drop in behavior change.'
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ['lib']
s.add_runtime_dependency 'minitest', '~> 2.1'
s.add_runtime_dependency 'rails', '~> 3.0'
s.add_development_dependency 'rake'
Gem::Specification.new do |gem|
gem.name = 'minitest-spec-rails'
gem.version = MiniTestSpecRails::VERSION
gem.platform = Gem::Platform::RUBY
gem.authors = ['Ken Collins']
gem.email = ['[email protected]']
gem.homepage = 'http:https://github.com/metaskills/minitest-spec-rails'
gem.summary = 'Drop in MiniTest::Spec support for Rails 3 or 4.'
gem.description = 'Force ActiveSupport::TestCase to subclass MiniTest::Spec for a drop in behavior change.'
gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.require_paths = ['lib']
gem.add_runtime_dependency 'minitest', '~> 4.3'
gem.add_runtime_dependency 'rails', '>= 3.0'
gem.add_development_dependency 'minitest-emoji'
gem.add_development_dependency 'pry'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'sqlite3'
end
Loading

0 comments on commit 6d5d3ae

Please sign in to comment.