简体   繁体   中英

Rails Rspec IntegrationTest Capybara

I have started to test my app via Rspec (Capybara). 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. the problem is, if I run 'rake spec:requests', I get this errors:

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

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. 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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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