Fred Perrin
Fred Perrin

Reputation: 1134

rspec doesn't see fields dynamically added or removed by cocoon in nested form

In my application in Rails 4, I use Cocoon to have nested forms.

It works well in the browser, but I want to add some tests with rspec.

If I make a click_link("add") (or 'remove') in my test, rspec doesn't see any modification (I print page.body before and after).

I also tried with a sleep(10) but it's the same.

How could I test if the action (add or remove a nested form) works well ?

Thanks

EDIT:

The scenario to test :

scenario "nested form" do
  user_sign_in

  contact = FactoryGirl.create(:contact)

  visit new_message_path
  expect(page).to have_text("New message")

  puts page.html

  click_link('Add an action')  # <------ click on the link to add a new nested form

  puts page.html     # <--- the page is the same as before the click

  expect(page).to have_text("New action")

  user_sign_out
end

Upvotes: 1

Views: 721

Answers (1)

Chris Peters
Chris Peters

Reputation: 18090

If you're looking to test JavaScript-related functionality, then you need to pass js: true as an option to the related scenarios:

scenario "nested form", js: true do
  user_sign_in
  contact = FactoryGirl.create(:contact)

  visit new_message_path
  expect(page).to have_text("New message")

  click_link('Add an action')
  expect(page).to have_text("New action")

  user_sign_out
end

In order to do this, you also need either the selenium-webdriver or capybara-webkit gems. My impression is that Selenium is easier to get started with, but the WebKit one runs much faster.

Note that you can also pass js: true to feature, context, or describe if you need to test several scenarios with JavaScript.

Upvotes: 1

Related Questions