Rails Rspec IntegrationTest Capybara

时间:2016-11-29 11:42:32

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

我已经开始通过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”,我必须先创建一名员工。 问题是,如果我运行'rake spec:requests',我会收到这个错误:

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!'

2 个答案:

答案 0 :(得分:0)

我不确定,但如果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

请通过inspect元素确保您的输入名称正确,以获取输入名称

我认为

fill_in "loginname", with: "username"

也许是

fill_in "user[loginname]", with: "username"

答案 1 :(得分:0)

正如其他人所说,Capybara测试的类型应该是&#39;特征&#39;但请求&#39;然而,这不是导致错误的主要原因。您的应用程序代码在模板呈现期间引发异常,然后您在当前版本的Capybara中遇到错误,并处理其初始化程序采用多个参数的异常。只要您不使用jRuby,就可以将Capybara版本锁定到2.10.0,您应该会看到应用程序正在引发的错误。如果您使用的是jRuby,或者您不想锁定旧版本,则可以指定使用Capybara的主分支

gem 'capybara', github: 'teamcapybara/capybara'

修复了错误。

作为旁注,当您未实际使用capybara-webkit驱动程序时(因为它目前仅支持Capybara 2.7.1),您已使用capybara-webkit标记了此问题,因此您可能想将标签更改为只是水豚。