Is it still possible to use test-unit in rails 4?

2019-06-15 18:44发布

After upgrading from Rails 3.2 to Rails 4, my app works, but my tests, written with test-unit, are a disaster.

Minitest is rumored to be "compatible" with test-unit. However, if I attempt to use the (now bundled) Minitest, there are a raft of differences - from the assert* statement names and parameters to (clearly) many other things things both large and subtle.

If I instead try to avoid Minitest and attempt to keep my test-unit gem in my Gemfile, rake test explodes, saying,

undefined method 'refute_predicate' for class 'ActiveSupport::TestCase'

This results from having called

require 'rails/test_help'

:(

I have been searching for a while but have not yet figured out any way to continue using test-unit. The only alternative would seem to be a (for us, massive) refactoring of our test code, since something like 80% of our tests are broken in some way or other by Minitest's incompatibilities with test-unit.

Yet reading i.e. http://weblog.rubyonrails.org/2013/6/25/Rails-4-0-final/ and http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-3-2-to-rails-4-0 I find no mention of this in substance - I feel like must be missing something.

Is it possible to make Minitest more compatible with test-unit in a systematic way? Or to continue using test-unit in Rails 4?

2条回答
Fickle 薄情
2楼-- · 2019-06-15 19:10

Are you subclassing: Test::Unit::TestCase and not ActiveSupport::TestCase?

Maybe try adding minitest_tu_shim to your test group:

https://github.com/seattlerb/minitest_tu_shim

or make sure to subclass ActiveSupport::TestCase

查看更多
劳资没心,怎么记你
3楼-- · 2019-06-15 19:10

Yes Rails support Test::Unit. Here is the guide for it.

http://guides.rubyonrails.org/testing.html

Use a global search & replace on your test cases to change the method names.

Here is an integration test:

class UserFlowsTest < ActionDispatch::IntegrationTest

Here is a controller test:

class UsersControllerTest < ActionController::TestCase

Try rails new app to copy the automatically generated helpers and framework files and see examples.

查看更多
登录 后发表回答