Rails 4 Rspec expect{}.to change not registering despite test working as planned
I've been struggling with the same issue. You wait logic might have flaws. I used this approach and it worked well for me.
Copypasted from this article - basically you need to create a helper:
# spec/support/wait_for_ajax.rbmodule WaitForAjax def wait_for_ajax Timeout.timeout(Capybara.default_max_wait_time) do loop until finished_all_ajax_requests? end end def finished_all_ajax_requests? page.evaluate_script('jQuery.active').zero? endendRSpec.configure do |config| config.include WaitForAjax, type: :featureend
Include all support files automatically in your spec_helper.rb
spec/support/**/*.rb in our spec_helper.rb
or do it manually, like me:
#rails_helper.rbrequire_relative 'support/wait_for_ajax'
Now my test passes!
require 'rails_helper'RSpec.feature "Call Me Back contact front feature spec >", :type => :feature, js: true dofeature 'valid form' do scenario "with name and phone" do visit '/' execute_script '$("#call-popup").fadeIn(300)' fill_in 'call_me_back_request_name', with: 'Clark Kent' fill_in 'call_me_back_request_phone', with: '1234445566' within '#new_call_me_back_request' do expect{ find("input[type='submit']").click wait_for_ajax #<- the magic is here! }.to change(Request, :count).by(1) end endend