Invalid RSpec cache

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

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

The application works fine, but I have problems with some automated tests. The problem arises in the RSpec test, which works with this JavaScript. There are several tests in my specification file that change the ruby ​​time of MyApp.config.timeout_time on the fly to test various scenarios. In my spec file, the first example passes and the rest fail.

I finally realized that this was happening because myTimer never updated by JavaScript. When the first test runs, JavaScript compiles using the current value set in Ruby. When I change the Ruby timer for the second test, RSpec still uses the previous value in JavaScript.

Is there a way to tell Sprockets / Rails that the file or part of the cache is invalid so that JavaScript is rebuilt? I don't want to disable caching altogether, I just need a way to invalidate application.js based on each test, if necessary.

I can β€œtouch” one of the JavaScript files in the file system to make the stars think the file has been modified, but I really don't want to.

+5
source share
1 answer

I do not know how to do what you asked. But you might consider setting myTimer to the value you want it to be in the specifications of the functions for which it should be a specific value.

Assuming you are using Capybara, you can run Javascript in the function specification with

 page.execute_script('// some Javascript') 

You may have to rearrange your Javascript first to put myTimer somewhere where the script can change it.

+1
source

All Articles