我的Capybara测试出了问题。当我使用Selenium支持(js: true
)运行规范时,它将执行指令,直到遇到click_on
方法,该方法命中create
控制器操作并返回错误消息:“ActionController::UnknownFormat at /category/items
ActionController::UnknownFormat
” ,这可能是respond_with(@items)
问题。实际上,create
方法在服务器上运行良好以及整个功能,但在测试中它崩溃了。更重要的是,item在数据库中创建,以便确认create
方法正常工作。
请求信息:
GET No GET data
POST commit "Do AJAX request"
item {"item_id"=>"12345"}
utf8 "✓"
COOKIES cookie_policy "accepted"
规格:
require 'spec_helper'
feature "Test in Capybara and Selenium", js: true do
background(:all) do
...seed data
end
scenario "my test scenario" do
visit root_path
visit '/category/1'
within first(".css_style") do
click_on("item_link")
end
# we are into item view
click_on("Do AJAX request") # error
visit another_view_path
end
end
控制器:
class ...::ItemsController < ...::BaseController
respond_to :html, except: ['create', ...]
respond_to :js, only: ['create', ...]
def create
...
@item.save
respond_with(@item)
end
查看:
事情观点:
<%= link_to("request", category_items_path(item: {thing_id: thing}), method: :post, class: 'add_item_button', remote: true, name: "animate_item") %>
create.js.erb:
if($('#add-item-button').length > 0){
location.reload(200);
}
spec_helper.rb
...
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'
require 'selenium-webdriver'
require 'rack/utils'
Capybara.app = Rack::ShowExceptions.new(Sklep::Application)
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.default_driver = :selenium
...
如果我忘了什么,请告诉我。