4

I have a controller named reports in my rails applications. This controller have 4 or 5 (reports) actions and not a very large file.

I am using mini-test for testing my application. The test file of this controller (reports_controller_test.rb) is too big file. I wanted to break down the test files only for each report so that I can write tests for each report separately. Can I really do split my 1 mini-test file into many test files.

PLSQL_007
  • 215
  • 2
  • 8
wasipeer
  • 1,015
  • 11
  • 23

2 Answers2

5
  • Make a folder in your controllers test folder (i.e reports)
  • Make .rb files with each report name
  • Controller Test Class name should be same in all files of that folder
    • In your case "ReportsControllerTest"
  • Run your test, they 'll merge on run time, and works as single file, but it'll help you to break large files into small one
Malik Shahzad
  • 6,703
  • 3
  • 37
  • 49
  • 9
    Something that should be noted here is that these tests can't have their own setup methods. For example, if I have `MyControllerTest` and `MySecondControllerTest`, and they both test `MyController`, only one setup method will be used from one of the tests since they are merged and one is overwritten. Seems like this requires the setup for all of those tests to either exist in one file, or within each test, which may not be ideal. – framauro13 Jul 07 '16 at 17:15
2

I know this question is a bit old, but Minitest is still heavily used, so I hope this could be useful for anyone, as I was slightly confused.

Break down files is in a lot of cases something that you should do. If the new files have the same class name than the others, then we will see the behavior reported by Malik (the files are merged) but also the restriction that @framauro13 talked about. Using this approach, you would only have one setup method available for all the files.

What really makes sense is to have completely isolated test files. So in this case, we have to break the convention of indicating the controller in the class name of the test file and indicate the controller that we want to test. It can be done this way:

class MyCustomName < ActionController::TestCase
    tests Some::Namespace::TheController
    ...
end

class OtherCustomName < ActionController::TestCase
    tests Some::Namespace::TheController
    ...
end

More can be found here in the section "Controller is automatically inferred".

Jacob
  • 1,886
  • 2
  • 25
  • 40
  • Isn't `ActionController::TestCase` [deprecated](https://github.com/rails/rails/issues/22496) for a few years already? – mlt Mar 08 '19 at 01:36