在 RSpec 自动化测试期间使链轮缓存无效 [英] Invalidate Sprockets cache during RSpec automated test

查看:46
本文介绍了在 RSpec 自动化测试期间使链轮缓存无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Rails 应用程序中,我有一个 .js.erb 文件,该文件有一个基于一些 Ruby 代码动态设置的变量.

In my Rails application, I have a .js.erb file that has a variable which gets dynamically set based on some Ruby code.

var myTimer = <%= MyApp.config.timeout_time * 1000 %>;

该应用运行良好,但我在某些自动化测试中遇到了问题.该问题出现在与此 JavaScript 一起使用的 RSpec 功能测试中.我的规范文件有许多测试可以动态更改 ruby​​ MyApp.config.timeout_time 时间以测试不同的场景.在我的规范文件中,第一个示例通过,其余示例失败.

The app works fine, but I'm having a problem in some automated tests. The problem occurs in an RSpec feature test that works with this JavaScript. My spec file has a number of tests that change the ruby MyApp.config.timeout_time time on the fly to test out different scenarios. In my spec file, the first example passes, and the rest fail.

我终于意识到这种情况正在发生,因为 myTimer 从未在 JavaScript 端更新.当第一个测试运行时,JavaScript 将使用在 Ruby 中设置的当前值进行编译.当我为第二个测试更改 Ruby 计时器时,RSpec 仍在使用 JavaScript 中的先前值.

I finally realized this is happening because myTimer never gets updated on the JavaScript side. When the first test runs, the JavaScript gets compiled using the current value as it's set in Ruby. When I change the Ruby timer for the second test, RSpec is still using the previous value in the JavaScript.

有没有办法告诉 Sprockets/Rails 使文件或缓存的一部分无效,以便重新构建 JavaScript? 一般我不想关闭缓存,我只是需要一种在需要时在每次测试的基础上使 application.js 失效的方法.

Is there a way to tell Sprockets/Rails to invalidate a file or a part of the cache so the JavaScript will get rebuilt? I don't want to turn off caching in general, I just need a way to invalidate application.js on a per-test basis when needed.

我或许能够触摸"文件系统上的一个 JavaScript 文件,让 sprocket 认为该文件已被更改,但我真的不想这样做.

I may be able to "touch" one of the JavaScript files on the file system to make sprockets think the file has been changed, but I don't really want to do that.

推荐答案

我不知道怎么做你问的.但是您可以考虑将 myTimer 设置为您希望它在需要它是特定值的功能规范中的值.

I don't know how to do what you asked. But you could consider setting myTimer to the value you want it to be in the feature specs that need it to be a specific value.

假设您使用 Capybara,您可以使用

Assuming you're using Capybara, you can run Javascript in a feature spec with

page.execute_script('// some Javascript')

您可能需要先重新排列您的 Javascript 以将 myTimer 放在脚本可以更改它的地方.

You might need to rearrange your Javascript a bit first to put myTimer somewhere where a script can change it.

这篇关于在 RSpec 自动化测试期间使链轮缓存无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆