简体   繁体   English

Rails Rspec集成测试水豚

[英]Rails Rspec IntegrationTest Capybara

I have started to test my app via Rspec (Capybara). 我已经开始通过Rspec(水豚)测试我的应用程序。 This is how I am doing it: 这就是我的做法:

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

Because of env namely "TEST-ENV" I have to create an employee at first. 由于env即“ TEST-ENV”,因此我必须首先创建一个雇员。 the problem is, if I run 'rake spec:requests', I get this errors: 问题是,如果我运行“ 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!'

I'm not sure but I think better if type = feature 我不确定,但我认为类型=功能更好

Sample 样品

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

Please make sure your input name is correct by inspect element to get the input name 请通过inspect元素确保您输入的名称正确,以获取输入名称

I think 我认为

fill_in "loginname", with: "username"

maybe be 也许是

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

As others have stated, Capybara tests should be of type 'feature' not 'request', however that's not the primary cause of your error. 正如其他人所述,水豚测试应该是“功能”类型而不是“请求”类型,但这不是造成错误的主要原因。 Your apps code is raising an exception during template rendering, and then you're running into a bug in the current version of Capybara with handling exceptions whose initializers take multiple parameters. 您的应用代码在模板渲染期间引发了异常,然后您遇到了当前版本的Capybara中的一个错误,该错误正在处理其初始化程序采用多个参数的异常。 As long as you're not using jRuby you can lock your Capybara version to 2.10.0 and you should see the correct error your app is raising. 只要您不使用jRuby,就可以将Capybara版本锁定为2.10.0,并且应该看到应用程序引发的正确错误。 If you are using jRuby, or if you prefer to not lock to an older version, you can specify to use the master branch of Capybara 如果您使用的是jRuby,或者您不想锁定到较旧的版本,则可以指定使用Capybara的master分支

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

which has the bug fixed. 已修复的错误。

As a side-note, you've tagged this question with capybara-webkit when you're not actually using the capybara-webkit driver (since it only supports up to Capybara 2.7.1 currently), so you might want to change the tag to just capybara. 附带说明一下,当您实际上不使用capybara-webkit驱动程序时(因为它目前仅支持Capybara 2.7.1),您已经用capybara-webkit标记了这个问题,因此您可能需要更改标记到水豚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM