1

我正在尝试创建这种情况:

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

流程是:

  1. 用户用名称填写输入;
  2. 点击提交按钮;
  3. 甜蜜警报出现并显示成功消息;
  4. 在甜蜜警报中单击“确定”;
  5. 使用 jQuery 创建一个新行并附加到 html 表中。

负责此流程的 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.

提前感谢您的帮助!

编辑 1

在@ThomasWalpole 的一些评论之后,我试图发现为什么<%= csrf_meta_tag %>不渲染相应的标签。我发现只是csrf_meta_tag打电话protect_against_forgery?来检查是否config.action_controller.allow_forgery_protectiontrue。由于我在测试环境中运行,config.action_controller.allow_forgery_protectionfalse. 我更改true并重新运行测试。csrf 元标记已呈现,但我仍然收到 http 状态 405 - ajax 调用后不允许的方法。

编辑 2

@ThomasWalpole 要求的我的路线:

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 

编辑 3

我的水豚配置:

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
4

0 回答 0