minitest-spec-rails 3.0.7 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. data/.gitignore +1 -0
  2. data/.travis.yml +7 -0
  3. data/CHANGELOG.md +46 -0
  4. data/Gemfile +3 -1
  5. data/Guardfile +7 -0
  6. data/README.md +195 -41
  7. data/Rakefile +4 -6
  8. data/lib/minitest-spec-rails.rb +8 -2
  9. data/lib/minitest-spec-rails/dsl.rb +52 -0
  10. data/lib/minitest-spec-rails/init/action_controller.rb +24 -0
  11. data/lib/minitest-spec-rails/init/action_dispatch.rb +17 -0
  12. data/lib/minitest-spec-rails/init/action_mailer.rb +26 -0
  13. data/lib/minitest-spec-rails/init/action_view.rb +38 -0
  14. data/lib/minitest-spec-rails/init/active_support.rb +28 -0
  15. data/lib/minitest-spec-rails/init/mini_shoulda.rb +47 -0
  16. data/lib/minitest-spec-rails/rails.rb +7 -0
  17. data/lib/minitest-spec-rails/version.rb +1 -1
  18. data/minitest-spec-rails.gemspec +21 -16
  19. data/test/cases/action_controller_test.rb +41 -0
  20. data/test/cases/action_dispatch_test.rb +48 -0
  21. data/test/cases/action_mailer_test.rb +42 -0
  22. data/test/cases/action_view_test.rb +44 -0
  23. data/test/cases/active_support_test.rb +43 -0
  24. data/test/cases/mini_shoulda_test.rb +38 -0
  25. data/test/dummy_app/app/controllers/application_controller.rb +7 -0
  26. data/test/dummy_app/app/controllers/users_controller.rb +8 -0
  27. data/test/dummy_app/app/helpers/application_helper.rb +2 -0
  28. data/test/dummy_app/app/helpers/foos_helper.rb +5 -0
  29. data/test/dummy_app/app/helpers/users_helper.rb +9 -0
  30. data/test/dummy_app/app/models/post.rb +6 -0
  31. data/test/dummy_app/app/models/user.rb +6 -0
  32. data/test/dummy_app/app/models/user_mailer.rb +10 -0
  33. data/test/dummy_app/app/views/user_mailer/welcome.erb +1 -0
  34. data/test/dummy_app/app/views/users/index.rhtml +2 -0
  35. data/test/dummy_app/config/boot.rb +126 -0
  36. data/test/dummy_app/config/database.yml +3 -0
  37. data/test/dummy_app/config/environment.rb +12 -0
  38. data/test/dummy_app/config/environments/test.rb +28 -0
  39. data/test/dummy_app/config/initializers/new_rails_defaults.rb +15 -0
  40. data/test/dummy_app/config/routes.rb +6 -0
  41. data/test/dummy_app/db/schema.rb +0 -0
  42. data/test/dummy_app/init.rb +13 -0
  43. data/test/dummy_app/log/.keep +0 -0
  44. data/test/dummy_app/public/.htaccess +40 -0
  45. data/test/dummy_app/public/404.html +30 -0
  46. data/test/dummy_app/public/422.html +30 -0
  47. data/test/dummy_app/public/500.html +30 -0
  48. data/test/dummy_app/script/console +3 -0
  49. data/test/dummy_app/script/generate +3 -0
  50. data/test/dummy_tests/application_controller_test.rb +93 -0
  51. data/test/dummy_tests/foo_helper_test.rb +12 -0
  52. data/test/dummy_tests/integration_test.rb +30 -0
  53. data/test/dummy_tests/user_mailer_test.rb +91 -0
  54. data/test/dummy_tests/user_test.rb +72 -0
  55. data/test/dummy_tests/users_controller_test.rb +23 -0
  56. data/test/dummy_tests/users_helper_test.rb +81 -0
  57. data/test/support/minitest.rb +7 -0
  58. data/test/support/shared_test_case_behavior.rb +30 -0
  59. data/test/test_helper.rb +12 -0
  60. data/test/test_helper_dummy.rb +8 -0
  61. metadata +238 -63
  62. data/lib/minitest-spec-rails/test_case.rb +0 -33
  63. data/lib/test/unit.rb +0 -9
  64. data/lib/test/unit/assertions.rb +0 -7
  65. data/lib/test/unit/testcase.rb +0 -23
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ *.log
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.3
4
+ before_install:
5
+ - gem update --system 1.8.25
6
+ - gem install bundler
7
+ - bundle --version
@@ -1,4 +1,50 @@
1
1
 
2
+ ## 4.3.8
3
+
4
+ * Less coupling to ActiveRecord ORM, works for MongoDB now. Thanks @kimsuelim
5
+
6
+
7
+ ## v4.3.7
8
+
9
+ * Fix helper test bug where calling methods in first context block blew up. Fixes #13.
10
+
11
+
12
+ ## v4.3.6
13
+
14
+ * Only require the freedom patches and autorun when Rails.env.test?
15
+
16
+
17
+ ## v4.3.5
18
+
19
+ * Make sure #described_class works in ActiveSupport::TestCase class level.
20
+
21
+
22
+ ## v4.3.4
23
+
24
+ * Add mini_should support and talk about matchers.
25
+
26
+
27
+ ## v4.3.3
28
+
29
+ * Fix MiniTest::Unit::TestCase hack for Rails 4, ignore in Rails 3.
30
+
31
+
32
+ ## v4.3.2
33
+
34
+ * Way better support for controller_class, mailer_class, and helper_class reflection.
35
+
36
+
37
+ ## v4.3.1
38
+
39
+ * Eager load controller_class, mailer_class, and helper_class.
40
+
41
+
42
+ ## v4.3.0
43
+
44
+ * All new MiniTest::Spec for Rails!!! Tested to the hilt!!!
45
+ * Track MiniTest's major/minior version number.
46
+
47
+
2
48
  ## v3.0.7
3
49
 
4
50
  * Must use MiniTest version ~> 2.1. As 3.x will not work.
data/Gemfile CHANGED
@@ -1,2 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
+
2
3
  gemspec
4
+
@@ -0,0 +1,7 @@
1
+ guard 'minitest' do
2
+ watch(%r|^lib/minitest-spec-rails/init/(.*)\.rb|) { |m| "test/cases/#{m[1]}_test.rb" }
3
+ watch(%r|^test/test_helper\.rb|) { "test" }
4
+ watch(%r|^test/test_helper_dummy\.rb|) { "test" }
5
+ watch(%r|^test/cases/(.*)_test\.rb|)
6
+ watch(%r|^test/dummy_tests/(.*)_test\.rb|)
7
+ end
data/README.md CHANGED
@@ -1,66 +1,220 @@
1
- # MiniTest::Spec For Rails
2
1
 
3
- This gem makes it easy to use the MiniTest Spec DSL in Rails 3 application tests.
2
+ <img src="http://cdn.metaskills.net/minitest-spec-rails.png" width="233" height="154" />
4
3
 
5
- # Advantages
4
+ # Make Rails Use MiniTest::Spec!
6
5
 
7
- 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.
8
6
 
9
- 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.
7
+ Rails 4 was on its way to using MiniTest::Spec as the superclass for ActiveSupport::TestCase. But in one of many reversals, the work was [pulled by this commit](http://github.com/rails/rails/commit/b22c527e65a41da59dbfcb078968069c6fae5086). DHH says that [Rails is omakase](http://david.heinemeierhansson.com/2012/rails-is-omakase.html) and I am cool with that. But since Ruby is wide open for us to change it and we are free in DHH's words to "freedom patch" what we see fit, we can fix Rails.
10
8
 
11
- # How it works
9
+ The minitest-spec-rails gem makes it easy to use the MiniTest::Spec DSL within your existing Rails 3 or 4 test suite. It does this by forcing ActiveSupport::TestCase to subclass MiniTest::Spec.
12
10
 
13
- 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.
11
+ [![Gem Version](https://badge.fury.io/rb/minitest-spec-rails.png)](http://badge.fury.io/rb/minitest-spec-rails)
12
+ [![Build Status](https://secure.travis-ci.org/metaskills/minitest-spec-rails.png)](http://travis-ci.org/metaskills/minitest-spec-rails)
13
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/metaskills/minitest-spec-rails)
14
14
 
15
- Full Details here:
16
15
 
17
- http://metaskills.net/2011/03/26/using-minitest-spec-with-rails/
16
+ ## Usage
18
17
 
19
- # Styles
18
+ Existing or new Rails 3 or 4 applications that use the default Rails testing structure can simply drop in the minitest-spec-gem and start writing their tests in the new spec DSL. Since MinitTest::Spec is built on top of MiniTest::Unit, a replacement for Test::Unit, all of your existing tests will continue to work.
20
19
 
21
- This <a href="http://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.
20
+ ### How is this different than MiniTest::Rails?
22
21
 
23
- Test::Unit Style:
22
+ To start off both Mike Moore (@blowmage) and I have worked together and we both LOVE MiniTest::Spec. Both projects aim to advocate MiniTest and make Rails integration as easy as possible. However, there are a few key differences in our projects. Some of these differences may go away in time too. As always, choose the tool you think fits your needs. So how, is minitest-spec-rails different than [minitest-rails](https://github.com/blowmage/minitest-rails)?
24
23
 
25
- assert_equal 100, foo
24
+ * We aim to leverage existing Rails test directories and files!
25
+ * No special test helper and/or generators.
26
+ * Easy migration path for existing Rails applications.
27
+ * How we go about freedom patching Rails.
26
28
 
27
- MiniTest::Spec Style:
29
+ So the goal of this project is to make Rails 3 or 4 applications just work as if rails-core had decided to support MiniTest::Spec all along. We believe that eventually that day will come and when it does, all your tests will still work! So bundle up and get started!
28
30
 
29
- foo.must_equal 100
31
+ ```ruby
32
+ gem 'minitest-spec-rails'
33
+ ```
30
34
 
31
- There are a few missing assertions available in Test::Unit that are changed or no longer available in MiniTest.
32
35
 
33
- * The method <code>assert_raise</code> is renamed <code>assert_raises</code>.
34
- * There is no method <code>assert_nothing_raised</code>. There are good reasons for this on Ryan's blog.
36
+ ## Test Styles
35
37
 
36
- # Alternate MiniTest::Spec Describe
38
+ This <a href="http://cheat.errtheblog.com/s/minitest">cheat sheet</a> shows both the MiniTest::Unit assertions along with the MiniTest::Spec assertion syntax. 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. For example, both of these would work in MiniTest::Spec and are interchangeable.
37
39
 
38
- 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.
40
+ ```ruby
41
+ # MiniTest::Unit Assertion Style:
42
+ assert_equal 100, foo
39
43
 
40
- # ./test/unit/post_test.rb
41
- require 'test_helper'
42
- describe Post do
43
- it "must be valid" do
44
- @post.must_be :valid?
45
- end
46
- end
44
+ # MiniTest::Spec Assertion Style:
45
+ foo.must_equal 100
46
+ ```
47
+
48
+ All existing Rails test cases that subclass ActiveSupport::TestCase will continue to work and I personally suggest that you still **use the subclass convention** vs the outer describe test case convention. However either will work.
49
+
50
+ ```ruby
51
+ require 'test_helper'
52
+ class UserTest < ActiveSupport::TestCase
53
+ let(:user_ken) { User.create! :email => 'ken@metaskills.net' }
54
+ it 'works' do
55
+ user_ken.must_be_instance_of User
56
+ end
57
+ end
58
+ ```
59
+
60
+ ```ruby
61
+ require 'test_helper'
62
+ describe User do
63
+ # This will work too.
64
+ end
65
+ ```
66
+
67
+ Just for reference, here is a full list of each of Rails test case classes and the matching describe alternative if one exists. Remember, names are important when using the describe syntax. So you can not have a mailer named `FooBar` and expect it to work with the outer describe spec style since there is no way to map the spec type based on an existing naming convention.
68
+
69
+ ```ruby
70
+ # Model Test
71
+ class UserTest < ActiveSupport::TestCase
72
+ end
73
+ describe User do
74
+ end
75
+
76
+ # Controller Test
77
+ class UsersControllerTest < ActionController::TestCase
78
+ end
79
+ describe UsersController do
80
+ end
81
+
82
+ # Integration Tests - Must use subclass style!
83
+ class IntegrationTest < ActionDispatch::IntegrationTest
84
+ end
85
+
86
+ # Mailer Test
87
+ class UserMailerTest < ActionMailer::TestCase
88
+ end
89
+ describe UserMailer do
90
+ end
47
91
 
48
- # ./test/functional/posts_controller_test.rb
49
- require 'test_helper'
50
- describe PostController do
51
- describe "on GET to :show" do
52
- before do
53
- get :show, id: "1"
54
- end
55
- it "returns a post object " do
56
- post = assigns(:post)
57
- post.wont_be_nil
58
- end
59
- end
92
+ # View Helper Test
93
+ class UsersHelperTest < ActionView::TestCase
94
+ end
95
+ describe UsersHelper do
96
+ end
97
+ ```
98
+
99
+
100
+ ## Extras
101
+
102
+ We have baked in a few extra methods behind the scenes to minitest-spec-rails. Most directly support our needs to reflect on described classes, however, they may be useful to you too when meta-programming on top of minitest-spec-rails.
103
+
104
+ ### #described_class
105
+ The `described_class` method is available both via a class method and an instance method in any Rails test case. It is guaranteed to work despite the described level too. This allows class level macros to be build, much like Shoulda. Remember, it can only do this if you follow Rails naming conventions for your tests.
106
+
107
+ ```ruby
108
+ class UserTest < ActiveSupport::TestCase
109
+ described_class # => User(id: integer, email: string)
110
+ it 'works here' do
111
+ described_class # => User(id: integer, email: string)
112
+ end
113
+ describe 'and' do
114
+ it 'works here too' do
115
+ described_class # => User(id: integer, email: string)
60
116
  end
117
+ end
118
+ end
119
+ ```
120
+
121
+ ### mini_shoulda
122
+
123
+ If you are migrating away from Shoulda, then minitest-spec-rails' mini_shoulda feature will help. To enable it, set the following configuration in your test environment file.
124
+
125
+ ```ruby
126
+ # In config/environments/test.rb
127
+ config.minitest_spec_rails.mini_shoulda = true
128
+ ```
129
+
130
+ Doing so only enables a few aliases that allow the Shoulda `context`, `should`, and `should_eventually` methods. The following code demonstrates the full features of the mini_shoulda implementation. It basically replaces the shell of [should-context](https://github.com/thoughtbot/shoulda-context) in a few lines of code.
131
+
132
+ ```ruby
133
+ class PostTests < ActiveSupport::TestCase
134
+ setup { @post = Post.create! :title => 'Test Title', :body => 'Test body' }
135
+ teardown { Post.delete_all }
136
+ should 'work' do
137
+ @post.must_be_instance_of Post
138
+ end
139
+ context 'with a user' do
140
+ should_eventually 'have a user' do
141
+ # ...
142
+ end
143
+ end
144
+ end
145
+ ```
146
+
147
+ If you are in the assertions provided by shoulda-context like `assert_same_elements`, then you may want to consider copying them [from here](https://github.com/thoughtbot/shoulda-context/blob/master/lib/shoulda/context/assertions.rb) and including them in `MiniTest::Spec` yourself. I personally recommend just replacing these assertions with something more modern. A few examples are below.
148
+
149
+ ```ruby
150
+ assert_same_elements a, b # From
151
+ a.sort.must_equal b.sort # To
152
+
153
+ assert_does_not_contain a, b # From
154
+ a.wont_include b # To
155
+ ```
156
+
157
+ ### Matchers
158
+
159
+ **I highly suggest that you stay away from matchers** since MiniTest::Spec gives you all the tools you need to write good tests. Staying away from matchers will make your code's tests live longer. So my advice is to stay away from things like `.should ==` and just write `.must_equal` instead. However, if matchers are really your thing, I recommend the [minitest-matchers](https://github.com/zenspider/minitest-matchers) gem. You can also check out the [valid_attribute](https://github.com/bcardarella/valid_attribute) gem built on top of minitest-matchers.
160
+
161
+ ```ruby
162
+ describe Post do
163
+ subject { Post.new }
164
+ it { must have_valid(:title).when("Hello") }
165
+ it { wont have_valid(:title).when("", nil, "Bad") }
166
+ end
167
+ ```
168
+
169
+
170
+ ## Gotchas
171
+
172
+ ### Assertion Methods
173
+
174
+ If you are upgrading from Test::Unit, there are a few missing assertions that have been renamed or are no longer available within MiniTest.
175
+
176
+ * The method `assert_raise` is renamed `assert_raises`.
177
+ * There is no method `assert_nothing_raised`. There are good reasons for this on [Ryan's blog entry](http://blog.zenspider.com/blog/2012/01/assert_nothing_tested.html).
178
+
179
+ ### Mocha
180
+
181
+ If you are using [Mocha](https://github.com/freerange/mocha) for mocking and stubbing, please update to the latest, 0.13.1 or higher so it is compatible with the latest MiniTest. If you do not like the deprecation warnings in older versions of Rails, just add this below the `require 'rails/all'` within your `application.rb` file :)
182
+
183
+ ```ruby
184
+ require 'mocha/deprecation'
185
+ Mocha::Deprecation.mode = :disabled
186
+ ```
187
+
188
+ ### Rails 3.0.x
189
+
190
+ If you are using minitest-spec-rails with Rails 3.0, then your controller and mailer tests will need to use the `tests` interface for the assertions to be setup correctly within sub `describe` blocks. I think this is a bug with `class_attribute` within Rails 3.0 only. So use the following patterns.
191
+
192
+ ```ruby
193
+ class UsersControllerTest < ActionController::TestCase
194
+ tests UsersController
195
+ end
196
+ class UserMailerTest < ActionMailer::TestCase
197
+ tests UserMailer
198
+ end
199
+ ```
200
+
201
+
202
+ ## Contributing
203
+
204
+ The minitest-spec-rails gem is fully tested from Rails 3.0 to 4 and upward. We run our tests on [Travis CI](http://travis-ci.org/metaskills/minitest-spec-rails) in both Ruby 1.8 and 1.9. If you detect a problem, open up a github issue or fork the repo and help out. After you fork or clone the repository, the following commands will get you up and running on the test suite.
205
+
206
+ ```shell
207
+ $ bundle install
208
+ $ bundle exec rake appraisal:setup
209
+ $ bundle exec rake appraisal test
210
+ ```
61
211
 
62
- # Issues
212
+ We use the [appraisal](https://github.com/thoughtbot/appraisal) gem from Thoughtbot to help us generate the individual gemfiles for each Rails version and to run the tests locally against each generated Gemfile. The `rake appraisal test` command actually runs our test suite against all Rails versions in our `Appraisal` file. If you want to run the tests for a specific Rails version, use `rake -T` for a list. For example, the following command will run the tests for Rails 3.2 only.
63
213
 
64
- 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.
214
+ ```shell
215
+ $ bundle exec rake appraisal:rails32 test
216
+ ```
65
217
 
218
+ Our current build status is:
219
+ [![Build Status](https://secure.travis-ci.org/metaskills/minitest-spec-rails.png)](http://travis-ci.org/metaskills/minitest-spec-rails)
66
220
 
data/Rakefile CHANGED
@@ -1,13 +1,11 @@
1
- require 'bundler'
1
+ require 'rubygems'
2
+ require 'bundler/gem_tasks'
2
3
  require 'rake/testtask'
3
4
 
4
- Bundler::GemHelper.install_tasks
5
-
6
- Rake::TestTask.new do |t|
5
+ Rake::TestTask.new do |t|
7
6
  t.libs = ['lib','test']
8
7
  t.test_files = Dir.glob("test/**/*_test.rb").sort
9
8
  t.verbose = true
10
9
  end
11
10
 
12
- task :default => [:test]
13
-
11
+ task :default => :test
@@ -1,4 +1,10 @@
1
+ module MiniTestSpecRails
2
+ end
3
+
4
+ require 'rubygems'
5
+ require 'active_support/all'
6
+ require 'active_support/test_case'
1
7
  require 'minitest/spec'
2
- require 'test/unit/testcase'
3
8
  require 'minitest-spec-rails/version'
4
- require 'minitest-spec-rails/test_case'
9
+ require 'minitest-spec-rails/dsl'
10
+ require 'minitest-spec-rails/rails' if Rails.env.test?
@@ -0,0 +1,52 @@
1
+ module MiniTestSpecRails
2
+ module DSL
3
+
4
+ RAILS_TEST_CASES = []
5
+
6
+ def self.included(klass)
7
+ klass.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ def before(type = nil, &block)
13
+ setup { self.instance_eval(&block) }
14
+ end
15
+
16
+ def after(type = nil, &block)
17
+ teardown { self.instance_eval(&block) }
18
+ end
19
+
20
+ def rails_test_cases
21
+ RAILS_TEST_CASES
22
+ end
23
+
24
+ def register_rails_test_case(test_case)
25
+ return if RAILS_TEST_CASES.include?(test_case)
26
+ RAILS_TEST_CASES.unshift(test_case)
27
+ end
28
+
29
+ def describing_class
30
+ ancestors.detect { |a| Class === a && rails_test_cases.include?(a.superclass) }
31
+ end
32
+
33
+ def described_class
34
+ begin
35
+ describing_class.name.gsub(/Test$/, '').constantize
36
+ rescue NameError
37
+ nil
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ def describing_class
44
+ self.class.describing_class
45
+ end
46
+
47
+ def described_class
48
+ self.class.described_class
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,24 @@
1
+ module MiniTestSpecRails
2
+ module Init
3
+ module ActionControllerBehavior
4
+
5
+ def self.included(klass)
6
+ klass.class_eval do
7
+ register_spec_type(/Controller( ?Test)?\z/i, self)
8
+ register_spec_type(self) { |desc| Class === desc && desc < self }
9
+ register_rails_test_case self
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def setup_controller_request_and_response
16
+ describing_class.tests described_class
17
+ super
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+
24
+ ActionController::TestCase.send :include, MiniTestSpecRails::Init::ActionControllerBehavior