由于database_cleaner故障导致Rails capybara测试失败

时间:2016-10-18 22:27:39

标签: ruby-on-rails ruby actionmailer database-cleaner

我已经安装了database_cleaner并且对我的所有测试都有效,除非它遇到这个......

scenario "is coach, signs up, email not in const, requests admin status" do
  user_sign_in(user1)
  visit my_profile_users_path
  click_button "Request Admin Status"

  expect(ActionMailer::Base.deliveries.count).to eq(1)
end

Rails返回     失败/错误:期望(ActionMailer :: Base.deliveries.count).to eq(1)        预期:1             得到:137        (使用==进行比较)

不确定为什么数据库清理程序在这个实例上失败或如何修复它。任何帮助,将不胜感激。

Gemfile.rb

group :test do
  gem 'coveralls', require: false
  gem 'database_cleaner'
  gem 'launchy', require: false
end

规格/支持/ database_cleaner.rb

# frozen_string_literal: true

RSpec.configure do |config|

  config.before(:each) do
    DatabaseCleaner.clean_with(:truncation)
  end

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

  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end

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

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

  config.after(:each) do
  end
end

rails_helper.rb

require 'spec_helper'
require 'rspec/rails'
require_relative "../spec/support/database_cleaner"

spec_helper.rb

require 'database_cleaner'

1 个答案:

答案 0 :(得分:0)

我相信我通过改变测试来解决这个问题。

scenario "is coach, signs up, email not in const, requests admin status" do
  user_sign_in(user1)
  visit my_profile_users_path
  click_button "Request Admin Status"

  expect(ActionMailer::Base.deliveries.last.subject).to have_content("#{user1.username} requested admin")
end