水豚:添加js:对功能规格为true会导致FrozenError:无法修改冻结的字符串

时间:2019-05-31 16:22:20

标签: ruby-on-rails capybara rspec-rails

ruby '~> 2.6.1' rails 5.2.3

我有一项功能规格,需要执行一些JavaScript才能填充日期选择器的alt字段。

规格:

require 'rails_helper'
Capybara.default_driver = :selenium
Capybara.javascript_driver = :selenium

RSpec.feature "Guest user creates a new order", js: true do

  scenario "order is valid" do
    area = create(:coverage_area)
    visit root_path

    click_on "Book Now"

    fill_in "zipcode", with: "98168"

    page.execute_script('document.querySelector(".datepicker").value = "9/20/2020"')
    .
    .
    .
  end

end

我已经明确指出,在功能规格上启用js:true会立即在第一个方案操作中导致以下错误:

1) Guest user creates a new order order is valid
     Failure/Error: visit root_path

     FrozenError:
       can't modify frozen String
`block (2 levels) in <top (required)>'

我的Gemfile

group :development, :test do
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'rspec-rails'
  gem 'rails-controller-testing'
  gem 'guard-rspec'
end


group :test do
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  gem 'webdrivers', '~> 4.0'
  gem "database_cleaner"
  gem "factory_girl_rails"
  gem "shoulda-matchers"
end

我的spec_helper.rb

require 'simplecov'
require 'webdrivers'
.
.
.

经过一番研究,我了解到默认情况下capybara使用rack test driver,它不支持javascript,但是,通过配置capybara以使用:selenium进行此规格测试的步骤不会对错误。

1 个答案:

答案 0 :(得分:1)

YMMV,但是,对于我的同事和我来说,重新安装firefox和geckodriver https://github.com/mozilla/geckodriver/releases对我们有用。