Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.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
Ruby Rspec不';在某些规格之后,无法清除工厂对象_Ruby_Rspec_Factory Bot - Fatal编程技术网

Ruby Rspec不';在某些规格之后,无法清除工厂对象

Ruby Rspec不';在某些规格之后,无法清除工厂对象,ruby,rspec,factory-bot,Ruby,Rspec,Factory Bot,我正在使用没有Rails和ActiveRecord的ClearRuby(Ruby2.3.5),并试图为某些服务编写规范。若我只运行文件的一部分(包括问题规范),它就通过了。如果我运行整个文件-问题规范失败 顺便说一下,我已经使用了rspec/retry,并且它设置了20次retry(我在日志中看到,问题规范在所有20次中都失败了) 规范rb # frozen_string_literal: true require 'spec_helper' require 'byebug' RSpec.d

我正在使用没有Rails和ActiveRecord的ClearRuby(Ruby2.3.5),并试图为某些服务编写规范。若我只运行文件的一部分(包括问题规范),它就通过了。如果我运行整个文件-问题规范失败

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

规范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时,我发现这个类王国有几十个对象,但是变量“all_kingdoms”和“invokents”包含的对象不超过10个

所以我在这里看到的曾经的根本原因——rspec没有清除在测试方法“运行”期间创建的王国,但如何推动它摧毁它们?我不使用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

您是否尝试过使用before:each而不是before:all?是的,但没有任何更改请显示您的spec_helper.rb文件已将spec_helper.rb添加到问题正文您尝试过使用此:?