如何从另一个应用程序的测试套件在一个应用程序中执行 RSpec 测试套件?

How can I execute an RSpec test suite in one app from another app's test suite?

我正在编写一个与测试过程集成的 Ruby Gem,我想编写集成测试以表明它可以端到端地工作。我包括一个使用 Gem.

的简单应用程序 (TestApp)

我的 gem 包含一个 spec 文件夹,其根目录中包含测试应用程序。测试应用程序将其测试包含在 features 文件夹中。 (当然还有其他东西)。

my_gem
  |
  - spec
  - test_app
      |
      - features

在添加新测试之前,如果我在 my_gem 目录中并执行 bundle exec rspec,我对 my_gem 的测试将按预期执行。如果我执行:

cd test_app
bundle exec features

我对 test_app 的测试按预期执行。现在,如果我在我的 my_gem 测试中添加一个测试:

describe "Test test suite"
    it "executes as expected"
        `bundle exec test_app/features`
    end
end

并在 my_gem 中执行测试 我得到以下信息:

C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/tzinfo-1.2.2/lib/tzinfo/data_source.rb:182:in `rescue in create_default_data_source': No source of timezone data could be found. (TZInfo::DataSourceNotFound)
Please refer to http://tzinfo.github.io/datasourcenotfound for help resolving this error.
        from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/tzinfo-1.2.2/lib/tzinfo/data_source.rb:179:in `create_default_data_source'
...

按照建议的 URL 中的说明进行操作,使我陷入了向我的 gem 添加依赖项的困境,否则我将不需要这些依赖项,也不想强迫最终用户使用 gem依赖。

除了上面的,我还试过:

exec('bundle exec rspec test_app/features')

Dir.chdir('test_app') do
    `bundle exec rspec features`
end

但每次我都会遇到同样的错误。有趣的是,当我从控制台的 my_gem 目录执行 bundle exec rspec test_app/features 时,我在没有额外测试的情况下得到了相同的错误,但是当我在没有 bundle exec 的情况下执行命令时却没有。在测试中删除 bundle exec 没有任何区别。

是什么导致了这些错误?有没有一种方法可以在我的 my_gem 测试中执行我的 test_app 测试套件,这样它们就不会导致错误?

我在这里找到了解决方案:http://makandracards.com/makandra/15649-how-to-discard-a-surrounding-bundler-environment

事实证明,测试是在我当前的环境下执行的(其他人可能能够更好地解释这一点)。解决方案是使用 Bundler.with_clean_env:

Bundler.with_clean_env do
    Dir.chdir('test_app') do
        `bundle exec rspec features`
    end
end