title: How can I test a rails 4 confirm dialog with Capybara and Poltergeist?
body: <p>I'm trying to test that a link to a destroy action throws a native browser <code>confirm</code> box with the correct message.</p>
  
  <p>The link is being generated using rails' <code>link_to</code>:</p>
  
  <pre><code>link_to 'Delete', user_path, method: :delete, data: { confirm: "Are you sure?" }
  </code></pre>
  
  <p>And generates the following html:</p>
  
  <pre><code>&lt;a data-confirm="Are you sure?" data-method="delete" href="/users/6" rel="nofollow"&gt;Delete&lt;/a&gt;
  </code></pre>
  
  <p>The functionality is working correctly in the browser, but I want to test for it in my <code>rspec</code> feature spec.</p>
  
  <p>I'm trying to stub out the browser's <code>confirm</code> function as described <a href="https://stackoverflow.com/questions/20485884/how-does-one-test-the-content-of-confirm-dialogs-using-poltergeist">here</a> and in <a href="https://github.com/teampoltergeist/poltergeist/issues/50#issuecomment-33277170" rel="noreferrer">this gist</a>, however I can't get it to work.</p>
  
  <pre><code>it 'requests confirmation', js: true do
    visit user_path(user)
    page.execute_script "
      window.confirmMsg = null;
      window.confirm = function(msg) { window.confirmMsg = msg; return true; };"
    click_link 'Delete'
    expect(page.evaluate_script('window.confirmMsg')).to eq('Are you sure?')
  end
  </code></pre>
  
  <p>Gives the following error from <code>rspec</code>:</p>
  
  <pre><code>Failure/Error: expect(page.evaluate_script('window.confirmMsg')).to eq('Are you sure?')
  
         expected: "Are you sure?"
              got: nil
  
         (compared using ==)
  </code></pre>
  
  <p>However, if I call a <code>confirm</code> directly via <code>page.execute_script</code>:</p>
  
  <pre><code>it 'requests confirmation', js: true do
    visit user_path(user)
    page.execute_script "
      window.confirmMsg = null;
      window.confirm = function(msg) { window.confirmMsg = msg; return true; };
      window.confirm('Are you sure?');"
    expect(page.evaluate_script('window.confirmMsg')).to eq('Are you sure?')
  end
  </code></pre>
  
  <p>Then the test passes.</p>
  
  <p>Also clicking the Delete link will cause the test to fail, even if <code>confirm</code> has been called directly for <code>page.execute_script</code>:</p>
  
  <pre><code>it 'requests confirmation', js: true do
    visit user_path(user)
    page.execute_script "
      window.confirmMsg = null;
      window.confirm = function(msg) { window.confirmMsg = msg; return true; };
      window.confirm('Are you sure?');"
    click_link 'Delete'
    expect(page.evaluate_script('window.confirmMsg')).to eq('Are you sure?')
  end
  </code></pre>
  
  <p>Gives the same error from <code>rspec</code>:</p>
  
  <pre><code>Failure/Error: expect(page.evaluate_script('window.confirmMsg')).to eq('Are you sure?')
  
         expected: "Are you sure?"
              got: nil
  
         (compared using ==)
  </code></pre>
  
  <p><strong>Why is the test failing? And, how can I test confirm dialogues correctly?</strong></p>
  
  <hr>
  
  <p><strong>Context:</strong></p>
  
  <p>I'm running my tests from a Vagrant virtual machine, which is <code>Ubuntu 12.04.4 LTS</code> and running <code>ruby 2.1.2p95</code>.</p>
  
  <p>My <code>Gemfile.lock</code> shows that I have the following versions:</p>
  
  <pre><code>rails (4.1.4)
  poltergeist (1.5.1)
  capybara (2.4.1)
  </code></pre>