简体   繁体   English

如何测试控制器操作中的简单错误

[英]How to test Controller Actions for simple mistakes

When scaffolding controllers it will create tests for that model, do the test have the ability to check for runtime errors for the whole page including rendering the .erb 当脚手架控制器将为该模型创建测试时,该测试是否能够检查整个页面的运行时错误,包括呈现.erb

If so can tests scan for common typos in the .erb for example checkbox instead of check_box 如果可以,测试可以扫描.erb中的常见错别字,例如checkbox而不是check_box

Because silly typos take a stupid amounts of time to figure out because the code looks right. 因为愚蠢的错字需要花费大量的时间才能弄清楚,因为代码看起来正确。

It would be good if there was a plugin that would use a service to check if it's a common typo or gotcha. 如果有一个插件可以使用服务来检查它是否是常见的错字或陷阱,那将是很好的。

In general I test the controller in the controller tests and the views in the view tests. 通常,我在控制器测试中测试控制器,并在视图测试中测试视图。

In more detail, I will test that given the right input to the controller, it produces the right output. 更详细地讲,我将测试给控制器正确的输入,它会产生正确的输出。 I usually mock out the model(s) involved and concentrate on what work matters inside the controller. 我通常会模拟涉及的模型,并专注于控制器内部重要的工作。 In my view tests I simply validate that the things on the page look like what I want them to look. 在我的视图测试中,我只是简单地验证页面上的内容看起来像我想要它们看起来的样子。 I also use Jasmine to test javascript when I have more complex interactions in that body of code. 当我在该代码体内进行更复杂的交互时,我还使用Jasmine测试javascript。

I put a lot of stock in this separation of tests. 在这种分离测试中,我投入了大量股票。

I use rspec for my model, view, controller, routing and request tests and I write failing tests and then implement the feature/method to make those tests go green. 我将rspec用于模型,视图,控制器,路由和请求测试,并编写失败的测试,然后实施功能/方法以使这些测试变为绿色。 And also I run rake spec before checking in. This gives me good coverage on the type of things you're concerned about. 另外,我在签入之前运行rake spec。这使我可以很好地了解您所关注的事情的类型。

There is also a gem called autotest that will run a test each time you save them (or perhaps some other granularity). 还有一个名为autotest的gem,每次保存时都会运行测试(或其他某种粒度)。 If your test is green it will run the entire suite. 如果您的测试是绿色的,它将运行整个套件。 I don't use it because I don't like how aggressively it does this but I have friends that swear by it. 我不使用它,因为我不喜欢它做得这么积极,但是我有朋友对此表示钦佩。

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

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