【问题标题】:Running rspec from within ruby multiple times within the same process在同一进程中多次从 ruby​​ 中运行 rspec
【发布时间】:2014-07-04 12:52:08
【问题描述】:

我正在创建一个测试自动化工具,该工具从 ruby​​ 中运行 rspec 测试,而不是从命令行。

我像这样运行我的测试,它运行并给我一个输出散列和通过/失败等。

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_helper 和文件?

谢谢 瑞克

【问题讨论】:

    标签: ruby ruby-on-rails-4 rspec rspec2 rspec-rails


    【解决方案1】:

    我遇到了类似的问题,必须在两次运行之间执行RSpec#reset 以确保所有内容都已清理完毕。

    http://www.rubydoc.info/github/rspec/rspec-core/RSpec#reset-class_method

    【讨论】:

      【解决方案2】:

      我也遇到了同样的问题,结果发现只用一行代码就可以解决:

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

      来源:http://codedecoder.wordpress.com/2013/01/10/undefined-method-visit-for-rspec-capybara/

      【讨论】:

        【解决方案3】:

        添加

        require 'spec_helper'
        

        到每个规范文件的顶部。

        【讨论】:

        • test 有这个并且从命令行的 bundle exec rspec 运行良好,并且在 rails 控制台的代码上面运行良好。但是它没有第二次运行,因为出现上述错误
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-23
        • 2021-06-01
        • 2019-07-06
        • 2018-10-27
        相关资源
        最近更新 更多