examples.txt 文件何时会在 Rspec 中创建?

When will the examples.txt file will be created in Rspec?

我想将 examples.txt 文件复制到另一个 examples_1.txt 文件中,这样在 运行 文件被覆盖时我可以得到另一个副本。

问题: 我有这样的代码。我在 after(:suite) 部分复制了 example.txt 文件,但出现错误。

我遇到了这个错误。当我检查时,这是因为 examples.txt 文件甚至不存在。关于何时创建 examples.txt 文件的任何想法,以便我可以在创建文件后复制它们。

  No such file or directory @ rb_sysopen - reports/examples.txt
# ./spec/spec_helper.rb:94:in `block in <top (required)>'
# ./spec/spec_helper.rb:89:in `<top (required)>'

这是它发生的地方: https://github.com/rspec/rspec-core/blob/e7c5d030966a7e8dad3e0a67c61920c4f2437c15/lib/rspec/core/runner.rb#L90

      # Configures and runs a spec suite.
      #
      # @param err [IO] error stream
      # @param out [IO] output stream
      def run(err, out)
        setup(err, out)
        return @configuration.reporter.exit_early(@configuration.failure_exit_code) if RSpec.world.wants_to_quit

        run_specs(@world.ordered_example_groups).tap do
          persist_example_statuses
        end
      end

如果您在同一个文件 (https://github.com/rspec/rspec-core/blob/e7c5d030966a7e8dad3e0a67c61920c4f2437c15/lib/rspec/core/runner.rb#L113) 中探索 run_specs,您将看到挂钩在该方法 returns 之前已经完成并且 examples.txt 被保留.没有人预料到您的情况,因此没有“after_persistence_file_saved”挂钩供您使用。

一些可能的选择是:

  • monkey patch def persist_example_status 方法并进行额外的复制(hacky,带有 monkeypatching 的所有问题),或者
  • 运行 rspec ... && cp reports/examples.txt reports.examples_1.txt(更简单,更少hacky,做的假设很少)。
  • 打开 rspec-core 的 PR,引入您需要的钩子(我认为它不太可能被接受,因为存在更简单的解决方案)

各有优缺点,但这些似乎不在本题的讨论范围之内。