我已经开始通过Rspec (Capybara)测试我的应用程序。我是这样做的:
require 'rails_helper'
RSpec.describe "Homepages", type: :request do
describe "GET / without login" , js: true do
before(:all) do
Employee.create(username: "username", password: "12345", password_confirmation: "12345")
end
it "works!" do
visit root_path
fill_in "loginname", with: "username"
fill_in "password", with: "12345"
click_button('sign_in')
end
end
end
由于env即"TEST-ENV“,我必须首先创建一个employee。问题是,如果我运行'rake spec:request‘,我会得到这个错误:
1) Homepages GET / without login works!
Got 0 failures and 2 other errors:
1.1) Failure/Error:
def initialize(template, original_exception)
super(original_exception.message)
@template, @original_exception = template, original_exception
@sub_templates = nil
set_backtrace(original_exception.backtrace)
end
ArgumentError:
wrong number of arguments (1 for 2)
#/.rvm/gems/ruby-2.1.1/gems/actionview-4.2.7/lib/action_view/template/error.rb:64:in `initialize'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara/session.rb:128:in `exception'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara/session.rb:128:in `raise'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara/session.rb:128:in `rescue in raise_server_error!'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara/session.rb:125:in `raise_server_error!'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara/session.rb:113:in `reset!'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara.rb:334:in `block in reset_sessions!'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara.rb:334:in `reverse_each'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara.rb:334:in `reset_sessions!'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara/rspec.rb:21:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# Capybara::CapybaraError:
# Your application server raised an error - It has been raised in your test code because Capybara.raise_server_errors == true
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara/session.rb:126:in `raise_server_error!'
发布于 2016-11-29 21:19:00
我不确定,但我认为如果type = feature会更好
示例
require "rails_helper"
RSpec.feature "Homepages", type: :feature do
before do
Employee.create(username: "username", password: "12345", password_confirmation: "12345")
end
context "GET / without login" do
scenario "works!", js: true do
visit root_path
fill_in "loginname", with: "username"
fill_in "password", with: "12345"
click_button('sign_in')
end
end
end
请通过检查元素来获取输入名称,以确保您的输入名称正确
我认为
fill_in "loginname", with: "username"
也许是吧
fill_in "user[loginname]", with: "username"
发布于 2016-11-29 23:42:30
正如其他人所说,Capybara测试的类型应该是'feature‘而不是'request',然而这并不是导致错误的主要原因。你的应用程序代码在模板渲染过程中会引发异常,然后你会在当前版本的Capybara中遇到一个处理异常的错误,这些异常的初始化器会接受多个参数。只要你没有使用jRuby,你就可以将你的Capybara版本锁定到2.10.0,你应该会看到你的应用程序引发的正确错误。如果您使用的是jRuby,或者您不想锁定到旧版本,则可以指定使用Capybara的主分支
gem 'capybara', github: 'teamcapybara/capybara'
它已经修复了错误。
作为附注,当您实际上没有使用capybara-webkit驱动程序时,您已经用capybara-webkit标记了这个问题(因为它目前只支持capybara 2.7.1 ),所以您可能想要将标记更改为仅仅是Capybara。
https://stackoverflow.com/questions/40865070
复制相似问题