I'm confused about what the various testing appliances in Ruby on Rails are for. I have been using the framework for about 6 months but I've never understood the testing part of it. The only testing I've used is JUnit3 in Java and that only briefly.
我对Ruby on Rails中各种测试设备的用途感到困惑。我已经使用这个框架大约6个月了,但是我从来都不了解它的测试部分。我所使用的惟一测试是Java中的JUnit3,而且这只是简单的测试。
Everything I've read about it just shows testing validations. Shouldn't the validations in rails just work? It seems more like testing the framework than testing the your code. Why would you need to test validations?
我读过的所有内容都显示了测试验证。难道rails中的验证不能正常工作吗?它看起来更像是测试框架,而不是测试代码。为什么需要测试验证?
Furthermore, the tests seem super fragile to any change in your code. So if you change anything in your models, you have to change your tests and fixtures to match. Doesn't this violate the DRY principle?
此外,这些测试似乎对代码中的任何更改都非常脆弱。因此,如果您更改模型中的任何内容,您必须更改测试和装置以匹配。这不违反干原则吗?
Third, writing test code seems to take alot of time. Is that normal? Wouldn't it just be faster to refresh my browser and see if it worked? I already have to play with my application just to see if it flows correctly and make sure my CSS hasn't exploded. Why wouldn't manual testing be enough?
第三,编写测试代码似乎要花很多时间。是正常的吗?刷新我的浏览器,看看它是否有效,不是更快吗?我已经需要使用我的应用程序,看看它是否正确地流动,并确保我的CSS没有爆炸。为什么手工测试不够呢?
I've asked these questions before and I haven't gotten more than "automated testing is automated". I am smart enough to figure out the advantages of automating a task. My problem is that costs of writing tests seem absurdly high compared to the benefits. That said, any detailed response is welcome because I probably missed a benefit or two.
我以前问过这些问题,但除了“自动测试是自动的”之外,我没有得到任何其他答案。我很聪明,能够发现自动完成任务的好处。我的问题是,与好处相比,写测试的成本似乎高得荒谬。也就是说,任何详细的回应都是受欢迎的,因为我可能错过了一两个好处。
6 个解决方案
#1
23
Shouldn't the validations in rails just work? It seems more like testing the framework than testing the your code. Why would you need to test validations?
难道rails中的验证不能正常工作吗?它看起来更像是测试框架,而不是测试代码。为什么需要测试验证?
The validations in Rails do work -- in fact, there are unit tests in the Rails codebase to ensure it. When you test a model's validation, you're testing the specifics of the validation: the length, the accepted values, etc. You're making sure the code was written as intended. Some validations are simple helpers and you may opt not to test them on the notion that "no one can mess up a validates_numericality_of
call." Is that true? Does every developer always remember to write it in the first place? Does every developer never accidentally delete a line on a bad copy paste? In my personal opinion, you don't need to test every last combination of values for a Rails' validation helper, but you need a line to test that it's there with the right values passed, just in case some punk changes it in the future without proper forethought.
Rails中的验证是有效的——事实上,Rails代码库中有单元测试来确保它。当您测试模型的验证时,您是在测试验证的细节:长度、可接受的值等等。有些验证是简单的助手,您可能会选择不测试它们,因为“没有人会把validates_numericality_of调用搞砸。”这是真的吗?每个开发人员都记得首先编写它吗?每个开发人员都不会不小心删除错误复制粘贴上的一行吗?在我个人看来,您不需要为Rails的验证助手测试最后的所有值组合,但是您需要一行代码来测试它是否存在并传递正确的值,以防将来某个朋克在没有适当的预先考虑的情况下更改它。
Further, other validations are more complex, requiring lots of custom code -- they may warrant more thorough testing.
此外,其他验证更复杂,需要大量的定制代码——它们可能需要更彻底的测试。
Furthermore, the tests seem super fragile to any change in your code. So if you change anything in your models, you have to change your tests and fixtures to match. Doesn't this violate the DRY principle?
此外,这些测试似乎对代码中的任何更改都非常脆弱。因此,如果您更改模型中的任何内容,您必须更改测试和装置以匹配。这不违反干原则吗?
I don't believe it violates DRY. They're communicating (that's what programming is, communication) two very different things. The test says the code should do something. The code says what it actually does. Testing is extremely important when there is a disconnect between those things.
我不认为它违反了干。他们在交流(这就是编程,交流)两个完全不同的东西。测试说代码应该做点什么。代码说明了它的实际功能。当这些东西之间没有联系时,测试是非常重要的。
Test code and application code are intimately linked, obviously. I think of them as two sides of a coin. You wouldn't want a front without a back, or a back without a front. Good test code reinforces good application code, and vice versa. The two together are used to understand the whole problem that you're trying to solve. And well written test code is documentation -- it shows how the application code should be used.
很明显,测试代码和应用程序代码是紧密联系在一起的。我认为它们是硬币的两面。你不会想要一个没有背部的正面,或者一个没有正面的后背。好的测试代码可以增强良好的应用程序代码,反之亦然。这两种方法被用来理解你要解决的整个问题。编写良好的测试代码是文档——它展示了应该如何使用应用程序代码。
Third, writing test code seems to take alot of time. Is that normal? Wouldn't it just be faster to refresh my browser and see if it worked? I already have to play with my application just to see if it flows correctly and make sure my CSS hasn't exploded. Why wouldn't manual testing be enough?
第三,编写测试代码似乎要花很多时间。是正常的吗?刷新我的浏览器,看看它是否有效,不是更快吗?我已经需要使用我的应用程序,看看它是否正确地流动,并确保我的CSS没有爆炸。为什么手工测试不够呢?
You've only worked on very small projects, for which that testing is arguably sufficient. However, when you work on a project with several developers, thousands or tens of thousands of lines of code, integration points with web services, third party libraries, multiple databases, months of development and requirements changes, etc, there are a lot of other factors in play. Manual testing is simply not enough. In a project of any real complexity, changes in one place can often have unforeseen results in others. Proper architecture helps mitigate this problem, but automated testing helps as well (and helps identify points where the architecture can be improved) by identifying when a change in one place breaks another.
您只在非常小的项目上工作过,对于这些项目来说,测试是足够的。然而,当您与几个开发人员、数千或数万行代码、与web服务的集成点、第三方库、多个数据库、几个月的开发和需求变更等一起工作时,还有许多其他因素在起作用。手工测试是远远不够的。在任何真正复杂的项目中,一个地方的变化常常会在其他地方产生无法预料的结果。适当的体系结构有助于缓解这个问题,但是自动化测试也可以帮助(并帮助识别出体系结构可以改进的地方),通过识别一个地方的变化会破坏另一个地方。
My problem is that costs of writing tests seem absurdly high compared to the benefits. That said, any detailed response is welcome because I probably missed a benefit or two.
我的问题是,与好处相比,写测试的成本似乎高得荒谬。也就是说,任何详细的回应都是受欢迎的,因为我可能错过了一两个好处。
I'll list a few more benefits.
我将列出更多的好处。
If you test first (Test Driven Development) your code will probably be better. I haven't met a programmer who gave it a solid shot for whom this wasn't the case. Testing first forces you to think about the problem and actually design your solution, versus hacking it out. Further, it forces you to understand the problem domain well enough to where if you do have to hack it out, you know your code works within the limitations you've defined.
如果您首先测试(测试驱动开发),您的代码可能会更好。我还没见过一个程序员给它一个有力的机会,但事实并非如此。测试首先迫使您考虑问题并实际设计解决方案,而不是将其排除。此外,它迫使您充分理解问题域,如果您确实需要破解它,那么您就知道您的代码在您定义的限制内工作。
If you have full test coverage, you can refactor with NO RISK. If a software problem is very complicated (again, real world projects that last for months tend to be complicated) then you may wish to simplify code that has previously been written. So, you can write new code to replace the old code, and if it passes all of your tests, you're done. It does exactly what the old code did with respect to the tests. For a project that plans to use an agile development method, refactoring is absolutely essential. Changes will always need to be made.
如果您有完整的测试覆盖范围,您可以不承担任何风险。如果软件问题非常复杂(同样,持续数月的真实项目往往比较复杂),那么您可能希望简化以前编写的代码。所以,您可以编写新的代码来替换旧代码,如果它通过了所有的测试,您就完成了。它所做的正是旧代码对测试所做的事情。对于计划使用敏捷开发方法的项目来说,重构是绝对必要的。总是需要做出改变。
To sum up, automated testing, especially test driven development, is basically a method of managing the complexity of software development. If your project isn't very complex, the cost may outweigh the benefits (although I doubt it). However, real world projects tend to be very complex, and the results of testing and TDD speak for themselves: they work.
总之,自动化测试,尤其是测试驱动开发,基本上是管理软件开发复杂性的一种方法。如果你的项目不是很复杂,成本可能会超过收益(尽管我对此表示怀疑)。然而,现实世界中的项目往往非常复杂,测试和TDD的结果不言自明:它们是有效的。
(If you're curious, I find Dan North's article on Behavior Driven Development to be very helpful in understanding a lot of the value in testing: http://dannorth.net/introducing-bdd)
(如果您好奇的话,我发现Dan North关于行为驱动开发的文章对理解测试中的许多价值非常有帮助:http://dannorth.net/- bdd)
#2
2
Tests should validate your application logic. Personally, I think my most important tests are the ones I run in Selenium. They check that what shows up in the browser is actually what I expect to see. However, if that's all I had, then I would find it hard to debug - it helps to have lower level tests as well and integration, functional, and unit tests are all useful tools. Unit tests let you check that the model behaves the way you expect it to (and that means every method, not just validatins). Validatins will certainly Just Work, but only if you get them right. If you get them wrong, they will Just Work, but not the way you expected. Writing a couple of lines of test is quicker than debugging later on.
测试应该验证您的应用程序逻辑。我个人认为我最重要的测试是用Selenium运行的。他们检查在浏览器中显示的内容实际上是我期望看到的。然而,如果这就是我所拥有的,那么我就会发现它很难调试——拥有较低的级别测试也很有帮助,集成、功能和单元测试都是有用的工具。单元测试允许您检查模型的行为是否符合预期(这意味着每个方法,而不仅仅是验证程序)。确认器肯定是有效的,但前提是你要正确。如果你让他们错了,他们只会工作,而不是你期望的方式。编写几行测试比以后调试要快。
A simple example like the one at http://wiseheartdesign.com/2006/01/16/testing-rails-validations just checks validations in a unit test. The O'Reilly article at http://www.oreillynet.com/pub/a/ruby/2007/06/07/rails-testing-not-just-for-the-paranoid.html?page=1 is a bit more complete (though still fairly basic).
一个简单的例子,比如http://wiseheartdesign.com/2006/01/16/testing-railsvalidations,它只是检查单元测试中的验证。O'Reilly在http://www.oreillynet.com/pub/a/ruby/2007/06/07/rails-test -not-just- to -paranoid.html?page=1更完整一些(尽管仍然是基本的)。
Automated testing is particularly useful in regression testing where you change something and run a suite of tests to check that you didn't break anything else.
自动化测试在回归测试中特别有用,在回归测试中,您可以更改一些内容并运行一组测试,以检查您没有破坏其他内容。
Tests are a form of repetition, but they don't violate DRY because they express things in a different way. A test says "I did X so Y should happen". Code says "X happened, so now I need to do Z, which happens to cause Y to happen". i.e. a test stimulates a cause and checks an effect, while code responds to a cause, and effects something.
测试是重复的一种形式,但它们不会违反DRY,因为它们以不同的方式表达东西。一个测试说"我做了X,所以Y应该发生"代码说"X发生了,所以现在我需要做Z,这恰好导致Y发生"例如,测试刺激一个原因并检查一个结果,而代码对一个原因做出反应,并产生一些结果。
#3
2
A lot of the testing tutorials and the sample tests created by the Rails generators are pretty lame and IMHO that can give the mistaken impression that you're supposed to test stupid stuff like the built in Rails methods, etc.
很多由Rails生成器创建的测试教程和示例测试都很差劲,会给人一种错误的印象,认为您应该测试一些愚蠢的东西,比如Rails方法中的内置测试等等。
Since Rails has it's own test suite, there's no point in you writing or running tests that only test built in Rails functionality. Your tests should exercise the code you're writing! :-)
既然Rails有自己的测试套件,那么编写或运行测试就没有必要只在Rails功能中进行测试。您的测试应该执行您正在编写的代码!:-)
As for the relative merit of running tests vs just refreshing in your browser.. The larger your app gets, the more of a pain in the ass it is to have to manually run through numerous scenarios and edge cases to make sure nothing in your application has broken. Eventually, you'll stop testing your entire application after each change and just start "spot testing" the areas you think should have been affected. Inevitably, you'll find something that used to work months ago that is now completely broken, and you have no certainty when it broke or which changes broke it. After that happens enough times... you'll come to value automated testing.... :-)
至于运行测试相对于在浏览器中刷新的优点……你的应用程序越大,你就越需要手动运行大量的场景和边缘案例,以确保你的应用程序中没有任何东西坏掉。最终,在每次更改之后,您将停止测试整个应用程序,而只需对您认为应该受到影响的领域进行“现场测试”。不可避免的是,你会发现几个月前还在工作的东西现在已经完全坏掉了,你无法确定它是什么时候坏掉的,或者是什么改变打破了它。之后发生了足够多的事情……你会重视自动化测试....:-)
#4
1
I haven't really used Rails much, but I would think that these automated tests would be useful as smoke tests to be sure that the thing you just did doesn't break something that you did last week. This will become increasingly important as your project grows.
我并没有很好地使用Rails,但是我认为这些自动化测试将会很有用,就像烟雾测试一样,确保你所做的事情不会破坏你上周所做的事情。随着项目的发展,这将变得越来越重要。
Also, writing the tests before you write the code (using the Test-Driven-Development model) will help you write the code better and faster, since the tests force you to fully think the problem through. It will also help you to know where to break up complex methods into smaller methods that you can test individually.
另外,在编写代码之前编写测试(使用测试驱动开发模型)将帮助您更好更快地编写代码,因为测试迫使您充分考虑问题。它还将帮助您了解如何将复杂的方法分解为更小的方法,您可以对这些方法进行单独的测试。
You are right, writing and maintaining tests takes a lot of time. Sometimes more time than the code itself. However, it can save you time in bug fixing and refactoring for the reasons above.
您是对的,编写和维护测试需要很多时间。有时比代码本身花费更多的时间。但是,由于上述原因,它可以为您节省修复和重构bug的时间。
#5
1
For example: I work on a 25000+ lines project (yes, in rails 1.2) and last monday I was told if I could make Users dissapear from every list except admin ones if they had "leave_date" attribute set to the past.
例如:我在一个2.5万行以上的项目中工作(是的,在rails 1.2中),上周一有人告诉我,如果用户的“leave_date”属性设置为过去,那么除了管理员以外,我是否可以让他们从每个列表中删除。
You can rewrite every list action (50+) to put a
您可以重写每个列表动作(50+)来放置a
@users.reject!{|u| Date.today > u.leave_date}
@users.reject。{你| |日期。今天> u.leave_date }
Or you can override the "find" method (DRY;-), but only if you have tests (on everything that finds users!) you will know you didn't break anything by overriding User#find !!
或者您可以重写“查找”方法(DRY;-),但是只有当您有测试(在所有找到用户的东西上!)时,您才会知道您没有通过覆盖用户#find来破坏任何东西!
#6
1
Everything I've read about it just shows testing validations. Shouldn't the validations in rails just work? It seems more like testing the framework than testing the your code. Why would you need to test validations?
我读过的所有内容都显示了测试验证。难道rails中的验证不能正常工作吗?它看起来更像是测试框架,而不是测试代码。为什么需要测试验证?
There's a good Railscast showing one way to test controllers.
有一个很好的Railscast展示了一种测试控制器的方法。
#1
23
Shouldn't the validations in rails just work? It seems more like testing the framework than testing the your code. Why would you need to test validations?
难道rails中的验证不能正常工作吗?它看起来更像是测试框架,而不是测试代码。为什么需要测试验证?
The validations in Rails do work -- in fact, there are unit tests in the Rails codebase to ensure it. When you test a model's validation, you're testing the specifics of the validation: the length, the accepted values, etc. You're making sure the code was written as intended. Some validations are simple helpers and you may opt not to test them on the notion that "no one can mess up a validates_numericality_of
call." Is that true? Does every developer always remember to write it in the first place? Does every developer never accidentally delete a line on a bad copy paste? In my personal opinion, you don't need to test every last combination of values for a Rails' validation helper, but you need a line to test that it's there with the right values passed, just in case some punk changes it in the future without proper forethought.
Rails中的验证是有效的——事实上,Rails代码库中有单元测试来确保它。当您测试模型的验证时,您是在测试验证的细节:长度、可接受的值等等。有些验证是简单的助手,您可能会选择不测试它们,因为“没有人会把validates_numericality_of调用搞砸。”这是真的吗?每个开发人员都记得首先编写它吗?每个开发人员都不会不小心删除错误复制粘贴上的一行吗?在我个人看来,您不需要为Rails的验证助手测试最后的所有值组合,但是您需要一行代码来测试它是否存在并传递正确的值,以防将来某个朋克在没有适当的预先考虑的情况下更改它。
Further, other validations are more complex, requiring lots of custom code -- they may warrant more thorough testing.
此外,其他验证更复杂,需要大量的定制代码——它们可能需要更彻底的测试。
Furthermore, the tests seem super fragile to any change in your code. So if you change anything in your models, you have to change your tests and fixtures to match. Doesn't this violate the DRY principle?
此外,这些测试似乎对代码中的任何更改都非常脆弱。因此,如果您更改模型中的任何内容,您必须更改测试和装置以匹配。这不违反干原则吗?
I don't believe it violates DRY. They're communicating (that's what programming is, communication) two very different things. The test says the code should do something. The code says what it actually does. Testing is extremely important when there is a disconnect between those things.
我不认为它违反了干。他们在交流(这就是编程,交流)两个完全不同的东西。测试说代码应该做点什么。代码说明了它的实际功能。当这些东西之间没有联系时,测试是非常重要的。
Test code and application code are intimately linked, obviously. I think of them as two sides of a coin. You wouldn't want a front without a back, or a back without a front. Good test code reinforces good application code, and vice versa. The two together are used to understand the whole problem that you're trying to solve. And well written test code is documentation -- it shows how the application code should be used.
很明显,测试代码和应用程序代码是紧密联系在一起的。我认为它们是硬币的两面。你不会想要一个没有背部的正面,或者一个没有正面的后背。好的测试代码可以增强良好的应用程序代码,反之亦然。这两种方法被用来理解你要解决的整个问题。编写良好的测试代码是文档——它展示了应该如何使用应用程序代码。
Third, writing test code seems to take alot of time. Is that normal? Wouldn't it just be faster to refresh my browser and see if it worked? I already have to play with my application just to see if it flows correctly and make sure my CSS hasn't exploded. Why wouldn't manual testing be enough?
第三,编写测试代码似乎要花很多时间。是正常的吗?刷新我的浏览器,看看它是否有效,不是更快吗?我已经需要使用我的应用程序,看看它是否正确地流动,并确保我的CSS没有爆炸。为什么手工测试不够呢?
You've only worked on very small projects, for which that testing is arguably sufficient. However, when you work on a project with several developers, thousands or tens of thousands of lines of code, integration points with web services, third party libraries, multiple databases, months of development and requirements changes, etc, there are a lot of other factors in play. Manual testing is simply not enough. In a project of any real complexity, changes in one place can often have unforeseen results in others. Proper architecture helps mitigate this problem, but automated testing helps as well (and helps identify points where the architecture can be improved) by identifying when a change in one place breaks another.
您只在非常小的项目上工作过,对于这些项目来说,测试是足够的。然而,当您与几个开发人员、数千或数万行代码、与web服务的集成点、第三方库、多个数据库、几个月的开发和需求变更等一起工作时,还有许多其他因素在起作用。手工测试是远远不够的。在任何真正复杂的项目中,一个地方的变化常常会在其他地方产生无法预料的结果。适当的体系结构有助于缓解这个问题,但是自动化测试也可以帮助(并帮助识别出体系结构可以改进的地方),通过识别一个地方的变化会破坏另一个地方。
My problem is that costs of writing tests seem absurdly high compared to the benefits. That said, any detailed response is welcome because I probably missed a benefit or two.
我的问题是,与好处相比,写测试的成本似乎高得荒谬。也就是说,任何详细的回应都是受欢迎的,因为我可能错过了一两个好处。
I'll list a few more benefits.
我将列出更多的好处。
If you test first (Test Driven Development) your code will probably be better. I haven't met a programmer who gave it a solid shot for whom this wasn't the case. Testing first forces you to think about the problem and actually design your solution, versus hacking it out. Further, it forces you to understand the problem domain well enough to where if you do have to hack it out, you know your code works within the limitations you've defined.
如果您首先测试(测试驱动开发),您的代码可能会更好。我还没见过一个程序员给它一个有力的机会,但事实并非如此。测试首先迫使您考虑问题并实际设计解决方案,而不是将其排除。此外,它迫使您充分理解问题域,如果您确实需要破解它,那么您就知道您的代码在您定义的限制内工作。
If you have full test coverage, you can refactor with NO RISK. If a software problem is very complicated (again, real world projects that last for months tend to be complicated) then you may wish to simplify code that has previously been written. So, you can write new code to replace the old code, and if it passes all of your tests, you're done. It does exactly what the old code did with respect to the tests. For a project that plans to use an agile development method, refactoring is absolutely essential. Changes will always need to be made.
如果您有完整的测试覆盖范围,您可以不承担任何风险。如果软件问题非常复杂(同样,持续数月的真实项目往往比较复杂),那么您可能希望简化以前编写的代码。所以,您可以编写新的代码来替换旧代码,如果它通过了所有的测试,您就完成了。它所做的正是旧代码对测试所做的事情。对于计划使用敏捷开发方法的项目来说,重构是绝对必要的。总是需要做出改变。
To sum up, automated testing, especially test driven development, is basically a method of managing the complexity of software development. If your project isn't very complex, the cost may outweigh the benefits (although I doubt it). However, real world projects tend to be very complex, and the results of testing and TDD speak for themselves: they work.
总之,自动化测试,尤其是测试驱动开发,基本上是管理软件开发复杂性的一种方法。如果你的项目不是很复杂,成本可能会超过收益(尽管我对此表示怀疑)。然而,现实世界中的项目往往非常复杂,测试和TDD的结果不言自明:它们是有效的。
(If you're curious, I find Dan North's article on Behavior Driven Development to be very helpful in understanding a lot of the value in testing: http://dannorth.net/introducing-bdd)
(如果您好奇的话,我发现Dan North关于行为驱动开发的文章对理解测试中的许多价值非常有帮助:http://dannorth.net/- bdd)
#2
2
Tests should validate your application logic. Personally, I think my most important tests are the ones I run in Selenium. They check that what shows up in the browser is actually what I expect to see. However, if that's all I had, then I would find it hard to debug - it helps to have lower level tests as well and integration, functional, and unit tests are all useful tools. Unit tests let you check that the model behaves the way you expect it to (and that means every method, not just validatins). Validatins will certainly Just Work, but only if you get them right. If you get them wrong, they will Just Work, but not the way you expected. Writing a couple of lines of test is quicker than debugging later on.
测试应该验证您的应用程序逻辑。我个人认为我最重要的测试是用Selenium运行的。他们检查在浏览器中显示的内容实际上是我期望看到的。然而,如果这就是我所拥有的,那么我就会发现它很难调试——拥有较低的级别测试也很有帮助,集成、功能和单元测试都是有用的工具。单元测试允许您检查模型的行为是否符合预期(这意味着每个方法,而不仅仅是验证程序)。确认器肯定是有效的,但前提是你要正确。如果你让他们错了,他们只会工作,而不是你期望的方式。编写几行测试比以后调试要快。
A simple example like the one at http://wiseheartdesign.com/2006/01/16/testing-rails-validations just checks validations in a unit test. The O'Reilly article at http://www.oreillynet.com/pub/a/ruby/2007/06/07/rails-testing-not-just-for-the-paranoid.html?page=1 is a bit more complete (though still fairly basic).
一个简单的例子,比如http://wiseheartdesign.com/2006/01/16/testing-railsvalidations,它只是检查单元测试中的验证。O'Reilly在http://www.oreillynet.com/pub/a/ruby/2007/06/07/rails-test -not-just- to -paranoid.html?page=1更完整一些(尽管仍然是基本的)。
Automated testing is particularly useful in regression testing where you change something and run a suite of tests to check that you didn't break anything else.
自动化测试在回归测试中特别有用,在回归测试中,您可以更改一些内容并运行一组测试,以检查您没有破坏其他内容。
Tests are a form of repetition, but they don't violate DRY because they express things in a different way. A test says "I did X so Y should happen". Code says "X happened, so now I need to do Z, which happens to cause Y to happen". i.e. a test stimulates a cause and checks an effect, while code responds to a cause, and effects something.
测试是重复的一种形式,但它们不会违反DRY,因为它们以不同的方式表达东西。一个测试说"我做了X,所以Y应该发生"代码说"X发生了,所以现在我需要做Z,这恰好导致Y发生"例如,测试刺激一个原因并检查一个结果,而代码对一个原因做出反应,并产生一些结果。
#3
2
A lot of the testing tutorials and the sample tests created by the Rails generators are pretty lame and IMHO that can give the mistaken impression that you're supposed to test stupid stuff like the built in Rails methods, etc.
很多由Rails生成器创建的测试教程和示例测试都很差劲,会给人一种错误的印象,认为您应该测试一些愚蠢的东西,比如Rails方法中的内置测试等等。
Since Rails has it's own test suite, there's no point in you writing or running tests that only test built in Rails functionality. Your tests should exercise the code you're writing! :-)
既然Rails有自己的测试套件,那么编写或运行测试就没有必要只在Rails功能中进行测试。您的测试应该执行您正在编写的代码!:-)
As for the relative merit of running tests vs just refreshing in your browser.. The larger your app gets, the more of a pain in the ass it is to have to manually run through numerous scenarios and edge cases to make sure nothing in your application has broken. Eventually, you'll stop testing your entire application after each change and just start "spot testing" the areas you think should have been affected. Inevitably, you'll find something that used to work months ago that is now completely broken, and you have no certainty when it broke or which changes broke it. After that happens enough times... you'll come to value automated testing.... :-)
至于运行测试相对于在浏览器中刷新的优点……你的应用程序越大,你就越需要手动运行大量的场景和边缘案例,以确保你的应用程序中没有任何东西坏掉。最终,在每次更改之后,您将停止测试整个应用程序,而只需对您认为应该受到影响的领域进行“现场测试”。不可避免的是,你会发现几个月前还在工作的东西现在已经完全坏掉了,你无法确定它是什么时候坏掉的,或者是什么改变打破了它。之后发生了足够多的事情……你会重视自动化测试....:-)
#4
1
I haven't really used Rails much, but I would think that these automated tests would be useful as smoke tests to be sure that the thing you just did doesn't break something that you did last week. This will become increasingly important as your project grows.
我并没有很好地使用Rails,但是我认为这些自动化测试将会很有用,就像烟雾测试一样,确保你所做的事情不会破坏你上周所做的事情。随着项目的发展,这将变得越来越重要。
Also, writing the tests before you write the code (using the Test-Driven-Development model) will help you write the code better and faster, since the tests force you to fully think the problem through. It will also help you to know where to break up complex methods into smaller methods that you can test individually.
另外,在编写代码之前编写测试(使用测试驱动开发模型)将帮助您更好更快地编写代码,因为测试迫使您充分考虑问题。它还将帮助您了解如何将复杂的方法分解为更小的方法,您可以对这些方法进行单独的测试。
You are right, writing and maintaining tests takes a lot of time. Sometimes more time than the code itself. However, it can save you time in bug fixing and refactoring for the reasons above.
您是对的,编写和维护测试需要很多时间。有时比代码本身花费更多的时间。但是,由于上述原因,它可以为您节省修复和重构bug的时间。
#5
1
For example: I work on a 25000+ lines project (yes, in rails 1.2) and last monday I was told if I could make Users dissapear from every list except admin ones if they had "leave_date" attribute set to the past.
例如:我在一个2.5万行以上的项目中工作(是的,在rails 1.2中),上周一有人告诉我,如果用户的“leave_date”属性设置为过去,那么除了管理员以外,我是否可以让他们从每个列表中删除。
You can rewrite every list action (50+) to put a
您可以重写每个列表动作(50+)来放置a
@users.reject!{|u| Date.today > u.leave_date}
@users.reject。{你| |日期。今天> u.leave_date }
Or you can override the "find" method (DRY;-), but only if you have tests (on everything that finds users!) you will know you didn't break anything by overriding User#find !!
或者您可以重写“查找”方法(DRY;-),但是只有当您有测试(在所有找到用户的东西上!)时,您才会知道您没有通过覆盖用户#find来破坏任何东西!
#6
1
Everything I've read about it just shows testing validations. Shouldn't the validations in rails just work? It seems more like testing the framework than testing the your code. Why would you need to test validations?
我读过的所有内容都显示了测试验证。难道rails中的验证不能正常工作吗?它看起来更像是测试框架,而不是测试代码。为什么需要测试验证?
There's a good Railscast showing one way to test controllers.
有一个很好的Railscast展示了一种测试控制器的方法。