打开 js 时功能规范中断:true - Capybara - Rails
Feature spec breaking when turning on js: true - Capybara - Rails
我的应用程序有一些关于水豚的功能规范。现在我有一个正在尝试测试的模态。模态正在使用 Javascript。但是当我 运行 我的规格。我得到这个非常奇怪的错误。在这里。
错误
Failure/Error: page.driver.browser.set_cookie("account_id=#{account_id}")
TypeError:
no implicit conversion of Symbol into Integer
# /Users/intern/.rvm/gems/ruby-2.3.1/gems/poltergeist-1.11.0/lib/capybara/poltergeist/browser.rb:313:in `[]'
# /Users/intern/.rvm/gems/ruby-2.3.1/gems/poltergeist-1.11.0/lib/capybara/poltergeist/browser.rb:313:in `set_cookie'
# ./spec/support/_request_macros.rb:20:in `set_current_account'
# ./spec/features/manage_accounts_spec.rb:101:in `block (3 levels) in <top (required)>'
# /Users/intern/.rvm/gems/ruby-2.3.1/gems/rspec-wait-0.0.9/lib/rspec/wait.rb:46:in `block (2 levels) in <top (required)>'
这是损坏的文件
def set_current_account(account_id)
page.driver.browser.set_cookie("account_id=#{account_id}")
end
这是我的测试
测试
it "redirects to the previous account if a user accesses an account they are not on" do
third_account = create(:account, name: "Third Account", users: [user])
set_current_account(third_account.id)
visit root_path
expect(current_account).to eq(third_account.id)
user.accounts.delete(account1)
click_on "Test Account"
expect(current_path).to eq(snitches_path)
expect(page).to have_content("You don't have access to that account!")
expect(current_account).to eq(third_account.id)
expect(page).to have_content("Third Account")
end
我在上面的上下文中有 js:true。
真不知道怎么了。希望你们中的一些人更熟悉水豚和 javascript 测试。让我知道你的想法
通过查看 poltergeist,您似乎正在使用 - https://github.com/teampoltergeist/poltergeist/blob/28cb0d6fd427424acaddc4800514c13efedcb199/lib/capybara/poltergeist/browser.rb the passed cookie needs to be a hash - and really you shouldn't be calling anything on page.driver.browser (99% of the time that means you're doing something you shouldn't be). You could call set_cookie
on page.driver
- https://github.com/teampoltergeist/poltergeist/blob/master/lib/capybara/poltergeist/driver.rb - 它需要 (name, value, options = {}) 所以
page.driver.set_cookie("account_id", account_id)
这将解决您当前遇到的问题。然而,在 page.driver 上调用某些东西意味着你可能有 50% 的时间做错了。你真的不应该手动设置 cookie 来伪造登录,而是你应该实际登录或将你正在使用的任何 auth 库设置为测试模式并以这种方式进行(例如,如果使用设计 - https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara)
此外,您的测试存在许多问题,可能导致测试不稳定。
visit
不能保证在返回之前等待页面完成加载,因此任何紧随其后的不基于屏幕上可见项目的断言都可能是不稳定的。
- 在支持 JS 的驱动程序中调用 current_account(假设这是一个应用程序方法),这意味着单独的线程和您的测试无法访问控制器数据,并不能很好地工作 - 相反只需检查页面上的可见项目。
click_on
不会等待它触发的操作完成,因此立即检查之后的路径将非常不稳定 - 而是使用 have_current_path 匹配器,它将重试
期待(页面).to have_current_path(snitches_path)
我的应用程序有一些关于水豚的功能规范。现在我有一个正在尝试测试的模态。模态正在使用 Javascript。但是当我 运行 我的规格。我得到这个非常奇怪的错误。在这里。
错误
Failure/Error: page.driver.browser.set_cookie("account_id=#{account_id}")
TypeError:
no implicit conversion of Symbol into Integer
# /Users/intern/.rvm/gems/ruby-2.3.1/gems/poltergeist-1.11.0/lib/capybara/poltergeist/browser.rb:313:in `[]'
# /Users/intern/.rvm/gems/ruby-2.3.1/gems/poltergeist-1.11.0/lib/capybara/poltergeist/browser.rb:313:in `set_cookie'
# ./spec/support/_request_macros.rb:20:in `set_current_account'
# ./spec/features/manage_accounts_spec.rb:101:in `block (3 levels) in <top (required)>'
# /Users/intern/.rvm/gems/ruby-2.3.1/gems/rspec-wait-0.0.9/lib/rspec/wait.rb:46:in `block (2 levels) in <top (required)>'
这是损坏的文件
def set_current_account(account_id)
page.driver.browser.set_cookie("account_id=#{account_id}")
end
这是我的测试
测试
it "redirects to the previous account if a user accesses an account they are not on" do
third_account = create(:account, name: "Third Account", users: [user])
set_current_account(third_account.id)
visit root_path
expect(current_account).to eq(third_account.id)
user.accounts.delete(account1)
click_on "Test Account"
expect(current_path).to eq(snitches_path)
expect(page).to have_content("You don't have access to that account!")
expect(current_account).to eq(third_account.id)
expect(page).to have_content("Third Account")
end
我在上面的上下文中有 js:true。
真不知道怎么了。希望你们中的一些人更熟悉水豚和 javascript 测试。让我知道你的想法
通过查看 poltergeist,您似乎正在使用 - https://github.com/teampoltergeist/poltergeist/blob/28cb0d6fd427424acaddc4800514c13efedcb199/lib/capybara/poltergeist/browser.rb the passed cookie needs to be a hash - and really you shouldn't be calling anything on page.driver.browser (99% of the time that means you're doing something you shouldn't be). You could call set_cookie
on page.driver
- https://github.com/teampoltergeist/poltergeist/blob/master/lib/capybara/poltergeist/driver.rb - 它需要 (name, value, options = {}) 所以
page.driver.set_cookie("account_id", account_id)
这将解决您当前遇到的问题。然而,在 page.driver 上调用某些东西意味着你可能有 50% 的时间做错了。你真的不应该手动设置 cookie 来伪造登录,而是你应该实际登录或将你正在使用的任何 auth 库设置为测试模式并以这种方式进行(例如,如果使用设计 - https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara)
此外,您的测试存在许多问题,可能导致测试不稳定。
visit
不能保证在返回之前等待页面完成加载,因此任何紧随其后的不基于屏幕上可见项目的断言都可能是不稳定的。- 在支持 JS 的驱动程序中调用 current_account(假设这是一个应用程序方法),这意味着单独的线程和您的测试无法访问控制器数据,并不能很好地工作 - 相反只需检查页面上的可见项目。
click_on
不会等待它触发的操作完成,因此立即检查之后的路径将非常不稳定 - 而是使用 have_current_path 匹配器,它将重试期待(页面).to have_current_path(snitches_path)