How to test a scenario that requires javascript to complete?
A project I'm working on fetches data from a 3rd party service and caches it locally along with specific info for my app.
My environment:
- I provide a search mechanism against the service's search API.
- When a user browses to the item page for the first time, the my server fetches the info from the service and stores it
- On subsequent requests the browser just requests the information from my server
.. however.. I want info from the 3rd party service to stay up to date.
My plan (feedback welcome!)
- On a subsequent request after page load, some JS will ping the 3rd party service to check for updates
- If the item has been updated, push an update to my copy server side
How to test this?
When I write a controller spec I don't think the page's javascript is executed (to fetch the since-updated item from the service)
I had quite a bit of trouble trying to describe this so a diagram might help.
Technology开发者_高级运维 I'm using: Rails/RSpec/Factory Girl/JQuery
Let's think out of the box a little to solve this.
Can you hit the 3rd Party Service from the Back End when the page is requested? Then you remove the dependance on JavaScript.
There are a few other methods that are better than this, like putting the "update item information" action into a queue, and then processing it in the background on the Back End. This would allow for the 3rd Party Service to not slow your site down.
It looks like there are a few ways to test this assuming my current solution.
Thoughtbot makes a library called capybara-webkit that will spin up webkit in the background and render/run your tests through it. Selenium is another.
github link
blog post
back on this project after a bit of a hiatus.. using capybara and :js => true seems to work - some tricky timing issues, but otherwise good to go
精彩评论