Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 无法使用rspec集成测试登录用户_Selenium_Ruby On Rails 4_Capybara_Integration Testing - Fatal编程技术网

Selenium 无法使用rspec集成测试登录用户

Selenium 无法使用rspec集成测试登录用户,selenium,ruby-on-rails-4,capybara,integration-testing,Selenium,Ruby On Rails 4,Capybara,Integration Testing,我正在尝试创建一个登录测试;该功能已经实现并运行良好,但我正在尝试为其创建一个测试。但是,用户模型在会话控制器中为空,但在测试代码中有内容。在下面的代码中,我添加了binding.pry(调试器)来检查用户模型 当我不使用webdriver(js:true)时,它也可以正常工作,如果它只是这样的话: post "/api/user/sign_in", { user: { email: user.email, password: user.password } } exp

我正在尝试创建一个登录测试;该功能已经实现并运行良好,但我正在尝试为其创建一个测试。但是,用户模型在会话控制器中为空,但在测试代码中有内容。在下面的代码中,我添加了binding.pry(调试器)来检查用户模型

当我不使用webdriver(js:true)时,它也可以正常工作,如果它只是这样的话:

post "/api/user/sign_in", {
  user: {
    email: user.email,
    password: user.password
  }
}

expect(response).to be_success
我以前一直在使用同样的测试代码,它工作得很好。我不确定这是否与我的前端是AngularJS有关

测试代码

require 'rails_helper'

describe Model, js: true do
  let(:model) { Model }
  let!(:user)  { create(:user) }

  it "Login" do
    binding.pry ### #user exists in User.all #####

    visit root_path
    find('#btn-signin').click
    find('#tab-signin').click

    fill_in 'user_email', with: user.email
    fill_in 'user_password', with: user.password

    find('#btn-login').click

    binding.pry #### user exists in User.all ####

    expect(page).to have_css '.profile-pic'
  end
end
会话控制器

def create
  puts User.pluck(:email).join(', ') #### user does not exists (it's actually blank) ####

  warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")

  render :json => current_user.to_json
end
宝石

group :development, :test do
  gem 'factory_girl_rails'
  gem 'pry'
  gem 'quiet_assets'
  gem 'rspec-rails', '~> 3.0.0'
end

group :test do
  gem 'codeclimate-test-reporter', require: nil
  gem 'shoulda-matchers'
  gem 'jasmine'

  gem 'capybara'
  gem 'selenium-webdriver', '~> 2.38.0'
end
导轨规格

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.include Capybara::DSL
  config.include FactoryGirl::Syntax::Methods

  config.use_transactional_fixtures = true

  config.include UserMacros
  config.infer_spec_type_from_file_location!
end
解决方案: GEMFILE

RAILS_HELPER.RB config.use\u transactional\u fixtures=false

  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

不知道在这种情况下,但我在过去也遇到过类似的问题,这通常与如何处理数据库连接有关。
使用交易性固定装置是否正确?嗨@froderik,答案是否定的。我将其设置为false,然后使用database_cleaner gem来修复它。我应该删除这个问题吗?谢谢最好是添加并接受自己的答案,以便其他开发人员能够找到解决方案
  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end