在同一进程中多次从ruby中运行rspec

在同一进程中多次从ruby中运行rspec,ruby,ruby-on-rails-4,rspec,rspec2,rspec-rails,Ruby,Ruby On Rails 4,Rspec,Rspec2,Rspec Rails,我正在创建一个测试自动化工具,它从ruby内部而不是从命令行运行rspec测试 我像这样运行我的测试,它运行并给我一个带有pass/fail等的输出哈希 config = RSpec.configuration json_formatter = RSpec::Core::Formatters::JsonFormatter.new(config.out) reporter = RSpec::Core::Reporter.new(json_formatter) config.instance_v

我正在创建一个测试自动化工具,它从ruby内部而不是从命令行运行rspec测试

我像这样运行我的测试,它运行并给我一个带有pass/fail等的输出哈希

config = RSpec.configuration

json_formatter = RSpec::Core::Formatters::JsonFormatter.new(config.out)
reporter =  RSpec::Core::Reporter.new(json_formatter)
config.instance_variable_set(:@reporter, reporter)

RSpec::Core::Runner.run(["#{Rails.root}/spec/test1_spec.rb"])

puts json_formatter.output_hash 
一切都很好,直到我再次运行它并得到错误

> undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_7:0x007ff3d343d978>
>的未定义方法“访问”#
在再次运行之前,如何重新加载/确保已加载所有spec_帮助程序和文件

谢谢 里克

添加

require 'spec_helper'

到每个spec文件的顶部。

我也有同样的问题,结果证明它只能用一行代码修复:

require 'capybara'
include Capybara::DSL # Add this line right after requiring capybara

来源:

我也有类似的问题,必须在两次运行之间执行
RSpec#reset
,以确保一切都已清理干净


测试有这个功能,可以从命令行的bundle exec rspec中正常运行,也可以从rails控制台中正常运行上述代码。但它不会再次运行,因为会得到上述错误