如何测试一个需要javascript才能完成的场景?
我正在开发的一个项目从第三方服务获取数据并将其与我的应用程序的特定信息一起缓存在本地。
我的环境:
- 我针对服务的搜索 API 提供搜索机制。
- 当用户第一次浏览项目页面时,我的服务器会从服务中获取信息并将其存储
- 在后续请求中,浏览器只会从我的服务器请求信息
..但是..我希望来自第 3 方服务的信息保持最新。
我的计划(欢迎反馈!)
- 在页面加载后的后续请求中,一些 JS 会 ping 3rd 方服务以检查更新
- 如果该项目已更新,则将更新推送到我的副本服务器端
< strong>如何测试这个?
当我编写控制器规范时,我认为页面的 javascript 没有被执行(从服务中获取更新后的项目),
我在尝试描述这一点时遇到了很大的麻烦所以图表可能会有所帮助。
我正在使用的技术:Rails/RSpec/Factory Girl/JQuery
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们跳出框框思考一下来解决这个问题。
当请求该页面时,您可以从后端访问第 3 方服务吗?然后您就可以消除对 JavaScript 的依赖。
还有一些其他方法比这更好,例如将“更新项目信息”操作放入队列中,然后在后端的后台处理它。这将使第三方服务不会减慢您的网站速度。
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.
假设我当前的解决方案,看起来有几种方法可以测试这一点。
Thoughtbot 制作了一个名为 capybara-webkit 的库,它将在后台启动 webkit 并通过它渲染/运行您的测试。硒是另一种。
github 链接
博客文章
经过一段时间的中断后回到这个项目..使用水豚和 :js => true 似乎有效 - 一些棘手的时间问题,但在其他方面很好
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