← Back to Upcase

Feature specs for AJAX'd content

Anyone know a way to reliably test this behavior?

I’ve tried soooo many different ways to make this work, and I just can’t find a way to do so without have brittle specs.

I’ve tried using thoughtbot’s wait_for_ajax blog post, but it doesn’t really solve the issue (in my experience it doesn’t correctly judge whether an AJAX request is pending or not).

What are you trying to test? I ran into a problem with Capybara’s expect(page).not_to have_css when I was testing the unsuccessful path of a feature and was wanting to make sure a new record hadn’t been added to a list. It was passing when it should have been failing and to add more confusion you could put…

expect(page).to have_css 'li'```

in the same feature right after each other and it would still pass. I read up and found that it was doing it because after certain actions Capybara will poll the page for a set amount of time to give the matcher time to pass. In the case above the ```.not_to have_css``` passes straight away as the it gets evaluated before the AJAX has had time to complete and then it will give the ```.to have_css``` enough time for the AJAX to complete and then pass. Note if the order is reversed...

```expect(page).to have_css 'li
expect(page).not_to have_css 'li'```

then the ```.not_to have_css``` will fail because the AJAX has been given chance to complete in the ```.to have_css````

In my case I found a way to have a useful test by using ```.to have_css``` check for an error flash that comes from the AJAX request before using ```.not_to have_css```.

Hope this helps you in someway.