Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何测试蚕茧宝石&x27;Rails中带Capybara和Minitest的s嵌套表单_Ruby On Rails_Ruby_Capybara_Capybara Webkit_Cocoon Gem - Fatal编程技术网

Ruby on rails 如何测试蚕茧宝石&x27;Rails中带Capybara和Minitest的s嵌套表单

Ruby on rails 如何测试蚕茧宝石&x27;Rails中带Capybara和Minitest的s嵌套表单,ruby-on-rails,ruby,capybara,capybara-webkit,cocoon-gem,Ruby On Rails,Ruby,Capybara,Capybara Webkit,Cocoon Gem,是否可以使用Minitest和Capybara来测试Rails中的多步表单?我在网上和Stackoverflow中读到的所有示例都使用Rspec。例如 看起来这应该是在Minitest中可以实现的。我正在尝试测试一个使用Cocoon gem的嵌套表单,如下所示: 单击“新建选项”按钮之前: 单击“新建选项”按钮后: 但是,我的测试在这一步一直失败: click_link 'New Option' 如果我在单击链接“新建选项”后添加“保存并打开页面”,浏览器将显示应通过单击链接“新建选项”显

是否可以使用Minitest和Capybara来测试Rails中的多步表单?我在网上和Stackoverflow中读到的所有示例都使用Rspec。例如

看起来这应该是在Minitest中可以实现的。我正在尝试测试一个使用Cocoon gem的嵌套表单,如下所示:

单击“新建选项”按钮之前:

单击“新建选项”按钮后:


但是,我的测试在这一步一直失败:

click_link 'New Option'
如果我在
单击链接“新建选项”
后添加“保存并打开页面”,浏览器将显示应通过
单击链接“新建选项”
显示的字段。不过,当我在开发服务器上手动测试它时,它就可以工作了。此
新选项
按钮由Cocoon使用以下命令生成:

<%= link_to_add_association 'New Option', f, :options %>
Minitest&Capybara测试Javascript似乎有问题,因为它似乎在“newoption”链接失败,而不是在那之后。我不知道这是与Minitest和Capybara相关的Javascript的问题,还是我在Minitest中没有正确访问字段。

感谢您指出问题出在
Capybara webkit
上。除了来自的一些帮助外,我还需要以下内容来更改测试通过:

将这些添加到我的
Gemfile

group :test do
  gem 'selenium-webdriver'
  gem 'webdrivers'
end
将此添加到我的
test\u helper.rb

class ActionDispatch::IntegrationTest
  Capybara.register_driver :selenium do |app|
    Capybara::Selenium::Driver.new(app, browser: :chrome)
  end

  Capybara.javascript_driver = :chrome

  Capybara.configure do |config|
    config.default_max_wait_time = 10 # seconds
    config.default_driver        = :selenium
  end
end
然后我的测试:

test 'Account owners can add a new product as well as options and choices' do
  visit new_product_path(as: @account_owner)
  assert_title 'Add New Product'
  assert_selector 'h1', text: 'Add New Product'
  fill_in 'product_name', :with => @new_product.name
  # Users can add product options through the nested form
  click_link 'New Option'
  within('.product_options_name') do
    first('input.form-control').set("#{@t_shirt_colors.name}")
  end
  click_link 'Add Choice'
  within('.product-option-choice') do
    first('input.form-control').set("#{@red_color_choice.name}")
  end
  click_button 'Create Product'
  assert_selector '.alert', text: 'Product was successfully created.'
end

你可以用minitest做任何事,你可以用RSpec做任何事——只是断言/期望语法不同而已。Cocoon的嵌套表单需要在页面上使用JS,因此您需要使用支持JS-的Capybara驱动程序。您是否真的在使用像您的标签所示的驱动程序那样的
capybara webkit
(它已经过时,相当于一个7或8年的浏览器,不会运行现代JS)?如果是这样的话-切换到selenium with chrome,这样你就可以在测试运行时真正看到浏览器中发生了什么。”但是,我的测试在这一步一直失败:“那里有什么错误?“你也应该展示一下你的测试。”托马斯沃尔波尔,谢谢
capybara webkit
确实是问题所在。将发布使测试正常运行所需的所有更改。感谢您指出这一点。@leemcally不客气-您可能还想看看幻影驱动程序,它具有大多数水豚网络工具包的额外功能-但它还没有达到V1,因此有一些粗略的边缘注:当特定范围内只有一个输入时,您可以使用
填充
,而无需定位器-因此而不是必须首先执行
('input.form control').set(“{@t\u shirt\u colors.name}”)
您只需执行
填充(使用:“{@t\u shirt\u colors.name}”)
这使您更清楚地看到,您填写的是一个文本字段,而不是可用于复选框/收音机等的
set
。除非您确实需要它,否则最好不要使用
first
,因为它返回的元素有一些限制。
test 'Account owners can add a new product as well as options and choices' do
  visit new_product_path(as: @account_owner)
  assert_title 'Add New Product'
  assert_selector 'h1', text: 'Add New Product'
  fill_in 'product_name', :with => @new_product.name
  # Users can add product options through the nested form
  click_link 'New Option'
  within('.product_options_name') do
    first('input.form-control').set("#{@t_shirt_colors.name}")
  end
  click_link 'Add Choice'
  within('.product-option-choice') do
    first('input.form-control').set("#{@red_color_choice.name}")
  end
  click_button 'Create Product'
  assert_selector '.alert', text: 'Product was successfully created.'
end