RoR、RSpec:想要验证页面上的每个链接是否都可以提供服务

发布于 2024-11-04 11:48:28 字数 280 浏览 0 评论 0原文

我正在我的 RoR 应用程序上加强 RSpec 测试。我想要一种简单的方法来验证单击页面上的每个链接是否得到正确处理,即生成到控制器的可识别路由,并在控制器中有一个方法来为其提供服务。

我可以为每个页面编写特定的测试,但最好让 RSpec 辅助函数自动查找页面上的所有链接并验证每个链接。 FWIW,可以安全地假设所有链接都在我的域中(没有指向外部站点的链接)。

我不会通过列举我尝试过的所有方法来扩大这个问题,而是会问:你会怎么做?看起来是一个常见且有用的功能。

  • FF

I'm beefing up RSpec tests on my RoR app. I'd like a simple way to verify that clicking on each link on a page is properly handled, i.e. generates a recognized route to a controller, and has a method in the controller to service it.

I can write specific tests for each page, but it would be preferable to have an RSpec helper function find all the links on a page automagically and verify each one. FWIW, it's safe to assume that all links are in my domain (there are no links to external sites).

Rather than bloat this question by enumerating all the things I've tried, I'll just ask: how would you do it? Seems like a common and useful function.

  • ff

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可遇━不可求 2024-11-11 11:48:28

我会用水豚问题...

 1. make list of  pages
 2. For each page
 3.     Visit page
 4.     get all links using xpath (included in capybaara)
 5.     For each link
 6.          do a Rspec test get url; response.should be_ok

I'd use Capybara prob...

 1. make list of  pages
 2. For each page
 3.     Visit page
 4.     get all links using xpath (included in capybaara)
 5.     For each link
 6.          do a Rspec test get url; response.should be_ok
滥情空心 2024-11-11 11:48:28

这种事情属于集成测试,您可以用 RSpec 或 Cucumber 编写。单独测试链接的价值有限 - 它会告诉您链接是否已损坏,但不会告诉您链接是否指向错误的位置。

在 Cucumber 中,你可以这样写:

Given I am on the home page
When I follow "Buy Stuff"
Then I should be on the shopping page

This kind of thing belongs in an integration test, which you could write either in RSpec or Cucumber. Testing a link on it's own is of limited value - it will tell you if the link is broken, but it won't tell you if the link is pointing to the wrong place.

In Cucumber, you could write something like:

Given I am on the home page
When I follow "Buy Stuff"
Then I should be on the shopping page
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文