如何在 Rails 3 中使用 Rspec 2 测试路由?
我找不到任何解释如何在 Rails 3 中测试路由的内容。即使在 Rspec 书中,它也没有很好地解释。
谢谢
I can't find anything explaining how to test routes in Rails 3. Even in the Rspec book, it doesn't explain well.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
rspec-rails Github 站点上有一个简短的示例。您还可以使用脚手架生成器来生成一些固定示例。例如,
rails gscaffold Article
应该生成如下内容:
There is a brief example on the rspec-rails Github site. You can also use the scaffold generator to produce some canned examples. For instance,
rails g scaffold Article
should produce something like this:
Zetetic 的答案解释了如何测试路线。这个答案解释了为什么你不应该这样做。
一般来说,您的测试应该测试向用户(或客户端对象)公开的行为,而不是测试提供该行为的实现。路由是面向用户的:当用户输入
http://www.mysite.com/profile
时,他并不关心它是否会转到 ProfilesController;相反,他关心的是他能看到自己的个人资料。因此,不要测试您是否要使用 ProfilesController。相反,设置一个 Cucumber 场景来测试当用户转到
/profile
时,他会看到他的姓名和个人资料信息。这就是你所需要的。再次强调:不要测试你的路线。测试你的行为。
Zetetic's answer explains how to test routes. This answer explains why you shouldn't do that.
In general, your tests should test the behavior exposed to the user (or client object), not the implementation by which that behavior is provided. Routes are user-facing: when the user types in
http://www.mysite.com/profile
, he doesn't care that it goes to ProfilesController; rather, he cares that he sees his profile.So don't test that you're going to ProfilesController. Rather, set up a Cucumber scenario to test that when the user goes to
/profile
, he sees his name and profile info. That's all you need.Again: don't test your routes. Test your behavior.