Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveSupport::TestCase vs Test::Unit::TestCase when unit testing rails

I recently noticed my test database is not being cleaned up after my tests run if my tests subclass Test::Unit::TestCase. If my tests subclass ActiveSupport::TestCase, everything is cleaned up properly.

Can anyone explain why, and/or provide an explanation of using one versus the other?

I am using shoulda and factory_girl.

Thanks.

like image 873
Lee Avatar asked Jan 18 '10 07:01

Lee


People also ask

How do I run a test case in Rails?

Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case. You can also run a particular test method from the test case by providing the -n or --name flag and the test's method name.

What is test cases in Rails?

They make sure that a section of an application, or a “unit”, is behaving as intended. In a Rails context, unit tests are what you use to test your models. Although it is possible in Rails to run all tests simultaneously, each unit test case should be tested independently to isolate issues that may arise.

What is Minitest in Ruby on Rails?

What is Minitest? Minitest is a testing suite for Ruby. It provides a complete suite of testing facilities supporting test-driven development (TDD), behavior-driven development (BDD), mocking, and benchmarking. It's small, fast, and it aims to make tests clean and readable.


1 Answers

If you take a look at the code, you'll see ActiveSupport::TestCase has a lot of of setup and utility functions for testing Rails. Older versions of Rails used to use Test::Unit::TestCase with a lot of mixins, but moved to subclassing a while ago.

If you're testing a Rails app, you should subclass ActiveSupport::TestCase or ActionController:TestCase for controllers. The generators will do this automatically, so you should not have to think about it most of the time.

like image 182
Luke Francl Avatar answered Oct 30 '22 21:10

Luke Francl