6

My Rails application includes a JavaScript modal that pops up 45 seconds after a user clicks on a link. As a result, my acceptance tests, which used to pass when there was no delay, are failing.

I originally tried to use the Timecop gem to fast-forward time in my Capybara acceptance test, but that did not work. When I added a sleep(45), however, that did work. Obviously, I can't have sleep(45) in my specs 3 times, but it's good to know what does work so I can get closer to that with a faster method.

What I've concluded from my experiments is that Ruby keeps track of time and Javascript keeps track of time and Timecop is fast-forwarding Ruby time but not Javascript time. Is there a way to fast-forward 45 seconds in my Capybara tests so that my Javascript event fires off?

Here is the function that is causing my tests to fail:

        $('.votable').one('click', '.vote', function() {
          $('.post-vote').
            delay(45000).
            queue(function() {
                $(this).dialog('open')
            });
        });

Again, when the delay is removed, my specs pass, but I need the delay. How can I stub this out in my Capybara tests? Or do I need to test the method with Jasmine?

4

1 に答える 1

2

最終的に、この問題に対する最も簡単な解決策は、.js ファイルを .js.erb ファイルにしてから、分岐を使用して非テスト環境での遅延のみを含めることでした。理想的ではありませんが、他のすべてのソリューションははるかに複雑であり、ビュー コードの見栄えを良くするためだけにテストを 45 秒遅くすることは絶対に望んでいませんでした。

最終的なコードは次のようになります。

$('.votable').one('click'). '.vote', function() {
  $('.post-vote').
   <% unless Rails.env.test? %>
     delay(45000).
   <% end %>
   queue(function() {
     $(this).dialog('open')
    });
 });
于 2013-03-21T21:54:37.003 に答える