ruby-on-railsautomated-testscapybaraminitestsystem-testing

Capybara has_content? block


Sometime I really need to check something in the database,

But if I immediate will go to database with u = User.last there is a chance that AJAX request wont finished and in the User.last - I'll find old record.

So for this I need some workaround, for example,

assert has_content? 'Success' do # here we access to database end

We check for Success message, which server response, and this is the sign, that we can go to our database. How can I achive this? Thanks all in advance.


Solution

  • You don't need a block for this just do one then the other. The content assertion will wait for the content to exist which will confirm the AJAX has completed and then you can check for the User

    assert_content 'Success' # will wait for 'Success' to appear
    u = User.last # won't happen until after 'Success has appeared and therefor the AJAX has completed'
    

    Note also the use of the Capybara provided assert_content (rather than assert has_content? ...). You generally want to prefer using the Capybara provided assertions rather than asserting on the result of a Capybara boolean method since the error messages on failure will be a lot clearer.