简体   繁体   中英

How should I test my Rails app?

In the past couple of days I've been making slow progress adding tests to an existing rails app I've been working on for a little bit.

I'm just trying to figure out how much and what kind of tests (unit, functional, integration) will be enough to save me time debugging and fixing deploys that break existing functionality.

I'm the only one working on this application. It's basically an inventory management database for a small company (~20 employees). I didn't add testing from the beginning because I didn't really see the point but I've have a couple of deploys screw up existing functionality in the last little bit, so I thought it might be a good thing to add.

Do I need to test my models and controllers individually AND perform integration testing? There seem to be developers who believe that you should just integration test and backtrack to figure out what's wrong if you get an error from there.

So far I'm using RSpec + Factory Girl + Shoulda. That made it pretty easy to set up tests for the models.

I'm starting on the controllers now and am more than a little bit lost. I know how to test an individual controller but I don't know if I should just be testing application flow using integration tests as that would test the controllers at the same time.

I use integration tests to test the successful paths and general failure paths, but for the edge cases or more in-depth scenarios then I write model/view/controller tests.

I usually write the tests before writing the functionality in the application but if you want to add tests afterwards then I recommend using something like SimpleCov to see which areas of your application need more testing and slowly build up the test coverage for your application.

Writing tests after the app is already written is going to be tedious. At a minimum you should be testing for integration cases (which will test most controller functions and views), and also model tests separately so that your model calls work as you think they should.

For integration testing I find Capybara easy to use.

As a rule of thumb, most of your tests need to be unit, some functional, few integration.

Rails adheres to this wholeheartedly and you should do the same.

At the minimum you have to have:

  • A unit test for each of your models , add a test case for each of your validations in each of them, and finally a last one to make the sure the model is saved. If you have stuff like dependant: :destroy , touch: true on related models ( has_many , belongs_to ), add test cases for them as well.
  • A functional test for each of your controllers . Test cases for each action. Some actions can have multiple responses (like in case of 422 errors) so add another case to test the error response. If you have a authorization filter, test them as well.

Then you can have an integration test for a typical new user flow. Like Sign Up -> Create Post -> View it -> logout. Another one is to make sure that unauthorized users cannot access your resources.

And from now, do not commit ANY code without the above mentioned tests.

The best time to plant a tree was 10 years ago, second best time is now , so start testing!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM