【问题标题】:Running Rspec from an entirely different sinatra app?从完全不同的 sinatra 应用程序运行 Rspec?
【发布时间】:2026-01-11 01:05:02
【问题描述】:

我目前正在尝试运行一个 sinatra 框架,该框架从 repo 下载代码,对代码执行 rspec,然后处理来自 RSpec 的结果。但是,当我从不同的应用程序调用它时,我无法让 rspec 运行。

我不断收到以下错误:

/Users/dir/.rvm/gems/ruby-1.9.3-p194@global/gems/bundler-1.1.5/lib/bundler/rubygems_integration.rb:147:in `block in replace_gem': rspec-core is not part of the bundle. Add it to Gemfile. (Gem::LoadError)
    from /Users/dir/.rvm/gems/ruby-1.9.3-p194/bin/rspec:22:in `<main>'

在运行大量输出之后,它基本上归结为 ruby​​ 在执行 bundle exec 之前没有切换到正确的目录。我已经尝试了几乎任何方法来让它工作但没有成功......似乎即使我更改目录,它也会继续尝试在我正在使用的应用程序上运行 RSpec,而不是我想要的应用程序去做吧。

  Dir.chdir('../target_app'){
    exe = "bundle exec rspec spec"
    `#{exe}`
  }

我也尝试过类似这样的反引号、exec 和 system(),但没有成功。

`cd ../target_app && bundle exec rspec spec`

我也尝试过 ChildProcess 之类的方法,但没有成功:

  p = ChildProcess.build('bundle', 'exec', 'rspec', 'spec')
  p.io.inherit!
  p.cwd = '../target_app'
  p.start

任何有关如何解决此问题的线索或帮助将不胜感激!

编辑:

我也试过bundle install --binstub --path vendor

使用此代码:

  Dir.chdir('../target_app'){
    puts `pwd`.chomp
    exe = "bin/rspec spec -o ../tmp.txt"
    puts exe
    `#{exe}`
  }

我得到这个输出:

/Users/me/dev/target_app
bin/rspec spec -o ../tmp.txt
/Users/me/.rvm/gems/ruby-1.9.3-p194@global/gems/bundler-1.1.5/lib/bundler/rubygems_integration.rb:223:in `block in replace_bin_path': can't find executable rspec (Gem::Exception)
    from bin/rspec:16:in `<main>'

【问题讨论】:

    标签: ruby sinatra rspec2


    【解决方案1】:

    为 Bundler bundle install --binstubs 生成 binstubs 并使用 binstub 代替 rspec,以便始终使用正确的 rspec 和 Gemfile。

    http://gembundler.com/v1.2/man/bundle-exec.1.html(有关 binstubs 的更多信息,请参见在线手册页)

    【讨论】:

    • +1。对于每个项目,我现在总是这样做。我还添加了--path vendor,它确实有助于将整个项目从我正在处理的任何其他事情中沙箱化。 YMMV
    • 我刚刚尝试过,但仍然没有成功。我已经按照这里的指示进行操作:robots.thoughtbot.com/post/15346721484/use-bundlers-binstubs,但我仍然遇到同样的错误。我想我需要从该文件夹中加载 gemfile,但我不确定这是否有效或者这是正确的方法
    • 您希望相对于您的初始应用程序而不是目标应用程序执行bin/rspec。在使用rspec = File.expand_path("./bin/rspec") 执行Dir.chdir 之前设置bin/rspec 的路径(如果可行,则基于File.dirname(__FILE__) 获取完整路径可能更可靠。
    最近更新 更多