在某些规格后,Rspec不会清除工厂对象

时间:2019-10-16 16:39:24

标签: ruby rspec factory-bot

我正在使用没有Rails和ActiveRecord的透明红宝石(红宝石2.3.5),并尝试编写某些服务的规范。而且,如果我只运行文件的一部分(包括问题说明),它就通过了。如果我运行整个文件-问题说明将失败。

顺便说一句,我已经使用了rspec / retry,它设置了20次重试(而且我在日志中看到,问题规格在所有20次中都失败了。)

ballot_spec.rb

# frozen_string_literal: true

require 'spec_helper'
require 'byebug'

RSpec.describe Ballot do
  describe '#run' do
    let(:kingdoms) { build_list(:kingdom, 6) }
    let(:service) { described_class.new(kingdoms) }
    before { allow(service).to receive(:hold_ballot) }
    it 'holds ballot if cannot_finish_ballot? returns true' do
      allow(service).to receive(:cannot_finish_ballot?).and_return(true)
      service.run
      expect(service).to have_received(:hold_ballot).at_least(:once)
    end
    it "doesn't hold ballot if cannot_finish_ballot? returns false" do
      allow(service).to receive(:cannot_finish_ballot?).and_return(false)
      service.run
      expect(service).not_to have_received(:hold_ballot)
    end
    it 'returns Struct object' do
      expect(service.run.class.superclass).to be Struct
    end
  end

  describe '#hold_ballot' do
    let(:all_kingdoms) { build_list(:kingdom, rand(2..10)) }
    let(:pretendents) { all_kingdoms.first(rand(2..all_kingdoms.count)) }
    let(:message) { build(:message, from: all_kingdoms.sample, to: all_kingdoms.sample) }
    let(:expected_message_count) { pretendents.count * all_kingdoms.count }
    let(:new_service) { described_class.new(pretendents) }

    before do
      allow(Message).to receive(:new).and_return(message)
      allow(message).to receive(:send)
      new_service.send('hold_ballot')
    end

\/\/ THE PROBLEM IS IN THIS SPEC \/\/
    it 'prepares messages to all existed kingdoms from every pretendent' do
      expect(Message).to have_received(:new).exactly(expected_message_count).times
    end
    it 'only 6 of messages will be selected to be sent' do
      expect(message).to have_received(:send).exactly(6).times
    end
    it 'resets Kingdoms' do
      allow(Kingdom).to receive(:reset)
      new_service.run
      expect(Kingdom).to have_received(:reset).at_least(:once)
    end
  end
end

当我运行rspec spec / services / ballot_spec.rb:26(用于测试整个方法'#hold_ballot')时,每个规范都通过了。 当我运行rspec spec / services / ballot_spec.rb时,出现故障:

  1) Ballot#hold_ballot prepares messages to all existed kingdoms from every pretendent
     Failure/Error: expect(Message).to have_received(:new).exactly(expected_message_count).times

       (Message (class)).new(*(any args))
           expected: 4 times with any arguments
           received: 260 times with any arguments

当我使用byebug时,我看到Kingdom类有许多对象,但是变量“ all_kingdoms”和“ pretendents”包含的对象不超过10个。

所以我曾经在这里看到过一次根本原因-rspec不能清除在测试方法'#run'期间创建的王国,但是如何推动它摧毁它们呢?我不使用ActiveRecord,不使用Rails,因此无法调整“重新加载!”或“破坏”。试图运行GC.start,但没有帮助。我能做什么?非常感谢! )

spec_helper.rb

# frozen_string_literal: true

require './models/kingdom.rb'
require './models/message.rb'
require './services/message_compose.rb'
require 'factory_bot'
require 'ffaker'
require 'capybara'
require 'rspec/retry'
require './spec/factories/kingdom.rb'
require './spec/factories/message.rb'

RSpec.configure do |config|
  config.verbose_retry = true
  config.display_try_failure_messages = true
  config.around :each do |ex|
    ex.run_with_retry retry: 20 unless ex.run_with_retry
  end
  config.include FactoryBot::Syntax::Methods

  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end
  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end
  config.shared_context_metadata_behavior = :apply_to_host_groups
end

0 个答案:

没有答案