简体   繁体   English

我该如何测试我的Rails应用程序?

[英]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. 在过去的几天里,我一直在慢慢地将测试添加到现有的rails应用程序中,我一直在努力。

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). 它基本上是一个小公司(约20名员工)的库存管理数据库。 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. 到目前为止,我正在使用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. 我通常在编写应用程序中的功能之前编写测试,但如果您想在之后添加测试,那么我建议使用SimpleCov之类的东西来查看应用程序的哪些区域需要更多测试,并慢慢为您的应用程序构建测试覆盖率。

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. 对于集成测试,我发现Capybara易于使用。

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. Rails全心全意地坚持这一点,你也应该这样做。

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. 如果你有类似dependant: :destroy东西dependant: :destroytouch: true相关模型上的touch: truehas_manybelongs_to ),也为它们添加测试用例。
  • 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. 某些操作可能有多个响应(例如422错误),因此添加另一个案例来测试错误响应。 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! 种植树木的最佳时间是10年前,现在是第二好的时间 ,所以开始测试!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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