怎么不输入"捆绑exec"使用Bundler binstubs& chruby?

时间:2015-11-22 00:00:22

标签: ruby rspec bundle zsh chruby

我使用chruby来管理我的Ruby版本,以及Bundler的binstubs以避免输入"捆绑exec"。我遇到了一些我不太了解的问题。这是我在我们的(Rails 4,Ruby 2.2.2)应用程序中运行测试套件时得到的结果:

$ rspec             
/Users/duncanmalashock/.rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- simplecov (LoadError)
    from /Users/duncanmalashock/.rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/duncanmalashock/ruby_projects/platform/spec/spec_helper.rb:3:in `<top (required)>'
    from /Users/duncanmalashock/.rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/duncanmalashock/.rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/duncanmalashock/ruby_projects/platform/spec/controllers/admin/admin_controller_spec.rb:1:in `<top (required)>'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/bin/rspec:23:in `load'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/bin/rspec:23:in `<main>'

当我运行$ bundle exec rspec时,套件运行正常。我一直在研究后现代的gem_home,并安装了它,但它没有给我我想要的结果。我在OSX Yosemite上使用zsh。

<。>在.zshrc中:

source /usr/local/share/chruby/chruby.sh
source '/usr/local/share/chruby/auto.sh'
source /usr/local/share/gem_home/gem_home.sh

export PATH=./.bundle/bin:$PATH
alias b='bundle install --path .bundle/gems --binstubs .bundle/bin'

2 个答案:

答案 0 :(得分:2)

只需在系统中安装simplecov gem(只需执行:gem install simplecov)并再次运行测试。它会起作用。

但是,使用bundle exec运行rake或rspec命令是一种更好的做法。因此,您可以确保命令在Gemfile

的上下文中运行

您可以将这些别名添加到.zshrc文件中:

alias ber="bundle exec rspec"
alias be="bundle exec"

然后,运行你的rspec测试:

ber

,或者

be rspec

答案 1 :(得分:0)

您的问题是,bundler已在项目本地安装了simplecov,并且您正在尝试运行全局系统rspec。这将在系统ruby库位置中查找simplecov。使用Ruby语言版本管理器不会改变这种行为。它只会使用版本化的ruby库位置。

如果您希望您的版本化Ruby一起使用rspecsimplecov,那么您需要使用提供的simplecov等效工具在版本化的ruby中安装gem

我不确定为什么你会发现运行bundle exec非常重要。您可以随时在.bashrc.bash_profile中创建别名。我有两个bundle exec,一个是be,另一个是bexec。我还有bundle exec railsbrails)和bundle exec rakebreak)的别名。在实践中,我发现这些与运行常规命令几乎没有什么不同。