I'm trying to create this scenario:
scenario 'creating a new item category' do
page.find("#launch-category-form-modal").click
expect(page).to have_selector("#category-form-modal.active")
fill_in("item_category[name]", with: "pudim")
find("#submit-category-form-modal").click
within ".sweet-alert.visible" do
find('button.confirm').trigger('click')
end
expect(page).to have_selector(".category-line:last-child td:first-child",
text: "pudim")
end
The flow is:
- the user fill the input with a name;
- click the submit button;
- the sweet alert appear with a success message;
- the click in 'ok' inside the sweet alert;
- a new line is created with jQuery and append to the html table.
The JS code thats responsible for this flow is:
$.ajax({
url: "/admin/item_categories",
method: "POST",
data: data,
success: function(data) {
if (data.status == true) {
swal(
'Success',
'Item Category created successfully',
'success'
);
var newLine = createItemCategoryLine(data.itemCategory);
$("#item-category-table-body").append(newLine);
$("#category-form-modal").modal('hide');
$("form[action='/admin/item_categories']")[0].reset();
} else if(data.status == false) {
swal(
'Error',
'Something went wrong!',
'error'
)
}
}
After clicking the submit button
, Capybara can't find the sweet alert. I tried used save_and_open_screenshot
and the sweet alert don't appear in screen. I also tried to byebug
in the controller but it's not called.
I'm using :selenium_chrome
.
Thanks in advance for any help!
Edit 1
After a few comments of @ThomasWalpole, I tried to discover why <%= csrf_meta_tag %>
don't rendered the corresponding tags. I discovered that csrf_meta_tag
just call protect_against_forgery?
to check if config.action_controller.allow_forgery_protection
is true
. Since I'm running in test environment, config.action_controller.allow_forgery_protection
is false
. I changed to true
and re-run the tests. The csrf meta tags was rendered, but I'm still getting http status 405 - method not allowed after the ajax call.
Edit 2
My routes as @ThomasWalpole requested:
item_categories_path GET /admin/item_categories(.:format)
item_categories#index
create_item_category_path POST /admin/item_categories(.:format)
item_categories#create
update_item_category_path PATCH /admin/item_categories/:id(.:format)
item_categories#update
delete_item_category_path DELETE /admin/item_categories/:id(.:format)
item_categories#destroy
Edit 3
My capybara configs:
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require "spec_helper"
require "rspec/rails"
# Add additional requires below this line. Rails is not loaded until this point!
require "capybara/rspec"
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
Capybara.register_driver :selenium_chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.javascript_driver = :selenium_chrome
# To ensure that browser tests can find the test server process,
# always include the port number in URLs.
Capybara.always_include_port = true