关于使用 RSpec 测试 Rails 控制器的两个问题
如何直接调用 before_filter 来测试它的实现?假设我想为 ApplicationController 编写一个规范,其中包含一些前置过滤器。测试它们的标准方法是什么?请注意,我不想测试它们是否已被调用 - 实际上我只想在单个规范中测试它们的实现一次。
我喜欢在控制器规格的顶部添加“render_views”,但是当我使用存根时,当它尝试获取存根对象上的 id 或其他属性时,我会收到各种奇怪的错误。您如何处理这个问题,以便在使用存根时确保您的视图是正确的?
我使用 RSpec。
How do you call a before_filter directly to test it's implementation? Let's say I want to write a spec for ApplicationController that contains a few before filters. What is the standard way to test them? Note, I don't want to test that they've been called - I actually just want to test their implementation once in a single spec.
I like having "render_views" at the top of my controller specs, but when I use stubs, I get all sorts of strange errors as it tries to get the id or other attributes on the stub object. How do you deal with this so that you can make sure your views are correct while using stubs?
I use RSpec.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
广告 1) 是合适的帮手吗?然后测试他们作为助手。如果这些方法在您的控制器中,那么它们是私有的,您可以在
send
的帮助下测试它们。广告2)
我也有 render_views,节省了我测试视图的时间。我不使用存根,而是使用来自 FactoryGirl 等的真实对象。当你的对象发生变化时,整个对象的模拟很糟糕,而且需要做很多额外的工作。工厂则不然。当然,你的测试会变得更慢,但你会测试得更深更广。
Ad 1) Are the proper helpers? Then test them as helpers. If the are methods in you controller then they are private you can test them with the help of
send
.Ad 2)
I have render_views as well, saves me some time on testing the views. I don't use stubs but real objects from FactoryGirl or the like. The whole mocking of objects sucks when your objects change and it's a lot of extra work. Factories aren't. Of course, your tests will get slower, but you'll be testing deeper and wider.