htunc
htunc

Reputation: 475

Rails, Capybara - click_link on remote links doesnt work

I'm using Capybara to test my project. But i have a problem. I have some remote forms on my project. They add records via ajax. When i'm testing with capybara it works well on development environment. It visits the page, fills in the form and submits. Booom, record has been added and test didnt fail. But when i run rspec with test environments i'm getting unknown format exception.

 1) add new address user adds new address
 Failure/Error: find("input[value='Adres Ekle']").click

 ActionController::UnknownFormat:
   Account::AddressesController#create is missing a template for this request format and variant.

   request.formats: ["text/html"]
   request.variant: []
 # ./spec/features/user_add_new_address_spec.rb:28:in `block (2 levels) in <top (required)>'

I've also tried to respond via js from controller like;

 def create
    request.format = :js
 end

Then it returns;

 1) add new address user adds new address
 Failure/Error: find("input[value='Adres Ekle']").click

 ActionController::UnknownFormat:
   Account::AddressesController#create is missing a template for this request format and variant.

   request.formats: ["text/javascript"]
   request.variant: []
 # ./spec/features/user_add_new_address_spec.rb:28:in `block (2 levels) in <top (required)>'

And my scenario if u want more info;

scenario 'user adds new address' do
  expect(page).to have_content 'Kayıtlı Adreslerim'
  find("a[title='Adres Ekle']").click
  expect(page).to have_content 'Yeni Adres Ekle'
  expect(page).to have_content 'Adres Başlığı'
  fill_in 'address[name]', with:'Izmir Ofisi'
  select('Izmir', :from => 'address[city_id]')
  fill_in 'address[address]', with: 'Lorem ipsum dolor sit amet.'
  find("input[value='Adres Ekle']").click # It submits remote: true form. 
  expect(page).to have_content 'Success!'
end

PS: my create action doesnt render something like that. its like;

def create
    @new_address = Address.new
    @address = @current_account.addresses.new(address_params)
    if @address.save
      @check = true
    else
      @check = false
    end
end

it renders: create.js.erb

<% if @check %>
  if($('.addresses').length) {
    $('.addresses').append('<%= j(render('account/addresses/address', address: @address)) %>');
  }
  if($('#did-addresses').length){
      $('#did-addresses').append("<%= "<option selected='true' value='#{@address.id}'>#{@address.name}</option>".html_safe %>").selectpicker('refresh');
  }
  $('#new-address').html('<%= j(render('account/addresses/form', new_address: @new_address)) %>');
  swal({
      type: 'success',
      title: "<%= t('response.success') %>",
      text: "<%= t('flash.actions.create.notice', resource_name: Address.model_name.human) %>",
      timer: 2000
  });
  quickview.close('#new-address');
<% else %>
  <% @address.errors.each do |error| %>

  <% end %>
<% end %>

$('.preloader').fadeOut();

Upvotes: 2

Views: 1038

Answers (2)

Astm
Astm

Reputation: 1720

I was facing the same case in rails 6 but I fixed it be adding js: true to the scenario and it automatically worked well.

  scenario 'Should delete the feature', js: true do
    # Your logic
    # Your expectations
  end

Upvotes: 1

Thomas Walpole
Thomas Walpole

Reputation: 49890

Since copying your development config over your test config fixed your issue, it sounds like you probably an error in one of your JS files. Normally in the test and production environment all of your JS assets get concatenated into one file which means an error in any one of them can prevent the code in the others from being executed. In the development environment each JS file is loaded separately which means an error in any file can only affect the rest of the code in that file. Check your the console in your browser for any JS errors when going to the page in question and fix them.

Upvotes: 0

Related Questions