I’m trying to test a link that calls a controller action using ajax. Essentially, when the user clicks on “Follow”, he will be associated with the company and a render partial will be executed with JS.
The problem is that it works correctly when I try it in development, but it does not respond in the tests. I’ve been trying in a lots of ways, and it looks that the call never gets to the controller.
Here you can see the test:
#spec/integration/following_spec.rb
it "should add the company to the ones followed by the user", :js => true do
find("#current_company").click_link "Follow"
sleep 2
@user.companies_followed.include?(@company).should be_true
end
The view:
#app/views/companies/_follow_button.html.slim
= link_to change_follow_state_company_path(@company), :method => :put, :remote => true, :id => "follow", :class => "btn_block light" do
' Follow
And the test configuration:
#spec/integration_helper.rb
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/rails'
Dir[Rails.root.join("spec/integration/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.use_transactional_fixtures = true
config.before do
clear_email_queue
end
end
Rails.cache.clear
You have to take into account that isn’t RSpec who is trying to make that AJAX call but capybara.
Also, what Rails is doing with that
method: :putandremote: trueis using UJS, which capybara withrack/testdoesn’t handle well out-of-the-box (because it implies javascript). That would probably also be true if you weren’t even usingremote: truebut onlymethod: :put.I bet if you use capybara-webkit that won’t happen:
If that works, is
rack/testwhat’s giving you the problem. That’s because it isn’t so good when dealing with javascript. In cucumber there’s something calledcapybara_javascript_emulationbut I wouldn’t rely on that when doing the tests.My approach: Switch the driver on tests that need javascript and rely on the naive
rack-teston simpler ones 🙂Also, use
spinach, for god’s sake! (orturnip, at least) 😀