简体   繁体   中英

Rails Rspec and capybara - using to perform a, um, sort of test

Check edit at bottom of page

My boss has a sitemap up- it's basically just every route as a link, with a button to click that says "valid?" or "ignore" which will mark it valid or ignore it on the page.

He asked me to manually go through and click each link, test that page isn't a 500 or 404, and then mark it valid if it isn't.

This seems silly to me, as it is basically just a user facing test for working routes.

I could, in the same time, write out routing specs in Rspec for all those, but I guess he wants some sort of documentation that this is happening on the front end for himself and users.

I was thinking a fun way to work around this boring clicking would be to do it with some programming WHILE writing the specs. Makes him happy, and also adds actual value and test to the app that can be reused.

Is there a way to, in a spec, write something like:

links = page.all('a.routing-links)
link.each do |link|
  link.click
  if page status != 404 || 500
    Link.find(id).update_attribute("verified", true)
  end 
end

I tried putting that in my spec, but when link.click hits an incorrect route, it stops the test (which makes sense, as that route is broken and this is a test.

What I'd like is to be able to take that error and use it to update the attribute of my model.

Am I going about this completely wrong? Any better ideas or inspiration?

Thanks

Edit

I agree with the poster who said this is better left to a script or rake task.

I'm a bit lost on how to write a script that will go to a page, find every link, record its status_code, and then find and update a model. Any suggestions or tips? Ideally it would be run within in the application, so that I could have access to my models and controllers.

Thanks

Personally I wouldn't actually put this in a spec since you're not actually expecting anything to fail.

Instead I'd create a quick script, or even rake task to run through the links as you described.

That being said, this article: http://agileleague.com/2012/12/rails-3-2-custom-error-pages-the-exceptions_app-and-testing-with-capybara/ details how to bypass the normal fail in these circumstances, namely:

In your config/environments/test.rb

config.consider_all_requests_local = false
config.action_dispatch.show_exceptions = true

Though this would affect all tests, which is quite possibly not what you want.

Also, a minor thing that you'd probably figure out in no time when testing this - you'll either need to revisit the list page after clicking the link, or rather relying on link clicks, you could visit the href instead which would be a bit quicker.

links = page.all('a.routing-links')
link.each do |link|
  visit link[:href]
  if page.status != 404 || 500
    Link.find(id).update_attribute("verified", true)
  end 
end

I haven't tested that, so not sure if it would work like that, but you should be able to get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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