我试图创建这种情况:
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
流程是:
负责此流程的JS代码是:
$.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'
)
}
}
点击submit button
后,Capybara无法找到甜蜜警报。我尝试使用save_and_open_screenshot
并且甜蜜警报不会出现在屏幕上。我也尝试在控制器中byebug
,但没有调用它。
我正在使用:selenium_chrome
。
提前感谢您的帮助!
在@ThomasWalpole的一些评论之后,我试图发现为什么<%= csrf_meta_tag %>
没有呈现相应的标签。我csrf_meta_tag
protect_against_forgery?
只需致电config.action_controller.allow_forgery_protection
,检查true
是否为config.action_controller.allow_forgery_protection
。由于我在测试环境中运行,false
为true
。我改为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
并重新运行测试。 csrf元标记已呈现,但我仍然获得http状态405 - 在ajax调用后不允许使用该方法。
我的路线@ThomasWalpole要求:
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
我的水豚配置:
{{1}}