【问题标题】:Guard-RSpec and Spring: command `cmd: spring rspec` doesn't run the specs within GuardGuard-RSpec 和 Spring:命令 `cmd: spring rspec` 不在 Guard 中运行规范
【发布时间】:2014-03-11 13:42:48
【问题描述】:

Guard-RSpec 在自述文件中提到,可以通过指定自定义 cmd 来使用 spring 运行规范:

guard :rspec, cmd: 'spring rspec' do
  # ...
end

这曾经工作得很好,直到我做了一个spring binstub --all,它改变了我的bin/spring 从......

#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'spring' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
  Pathname.new(__FILE__).realpath)

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path('spring', 'spring')

...到...

#!/usr/bin/env ruby

# This file loads spring without using Bundler, in order to be fast
# It gets overwritten when you run the `spring binstub` command

unless defined?(Spring)
  require "rubygems"
  require "bundler"

  if match = Bundler.default_lockfile.read.match(/^GEM$.*?^    spring \((.*?)\)$.*?^$/m)
    ENV["GEM_PATH"] = ([Bundler.bundle_path.to_s] + Gem.path).join(File::PATH_SEPARATOR)
    ENV["GEM_HOME"] = ""
    Gem.paths = ENV

    gem "spring", match[1]
    require "spring/binstub"
  end
end

现在,当运行guard 并按回车键时,它只会告诉我:

[2] guard(main)>                     <<<<< pressing enter
14:35:35 - INFO - Run all
14:35:35 - INFO - Running all specs

并出现类似“RSpec results - Failed”的通知。

像这样更改我的Guardfile 并从 RSpec 的cmd 中删除spring 时...

guard :rspec, cmd: 'rspec' do

...再次运行规范,但显然没有使用弹簧?

我还必须提到,当从 OSX 终端运行 spring 时,似乎什么也没发生:

$ spring
$ 

那么:我必须如何配置 Guard 和 RSpec 才能使用 Spring?

更新

目前,我已将我的 bin/spring 可执行文件恢复为“binstubbing”之前的版本:

#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'spring' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
  Pathname.new(__FILE__).realpath)

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path('spring', 'spring')

Guardfile 看起来像这样:

guard :rspec, cmd: 'spring rspec' do ... end

这可行,但我不认为它比运行 rspec 更快。

所以我现在完全不确定如何使用 Spring 正确运行 RSpec - 使用 spring rspec 还是仅使用 rspec

【问题讨论】:

  • 试试cmd: 'spring rspec spec'。由于 Spring 更新到 1.1.0,它似乎不再默认选择 spec 目录。
  • 谢谢,但这对我来说没有任何改变。

标签: ruby-on-rails spring rspec guard


【解决方案1】:

我之前正在研究这个问题。

Binstub 是可执行文件的包装脚本。他们生活在 Rails 中 在 bin/.如果你运行 spring binstub --all,你的 binstubs 将是 使用弹簧。

http://makandracards.com/makandra/26083-3-ways-to-run-spring-the-rails-app-preloader-and-how-to-disable-it

鉴于这一事实,您应该能够做这样的事情来将 Rspec 与 Spring 一起使用

guard :rspec, cmd: "bin/rspec" do

一个小测试来验证。确保你已经有 binstubbed rspec。

bundle binstubs 'rspec-core'

验证 Spring 没有在 bin/rspec 中引导。不应出现以下块。

[bin/rspec]

begin
  load File.expand_path("../spring", __FILE__)
rescue LoadError
end

关闭 Spring。运行 bin/rspec 并验证 Spring 没有被触发。

spring stop
spring status
bin/rspec
spring status

如果 Spring 没有被触发,你应该会看到这个

Spring is not running.

现在使用 Spring 引导您的 binstub。确保您已经捆绑安装了必要的 gem。

[宝石文件]

group :development, :test do
  gem "rspec-rails", "~> 3.0"
  gem 'spring-commands-rspec'
  ...
end

[终端]

bundle install

更新 binstubs 以使用 Spring

spring binstub --all

验证 Spring 是否已在 bin/rspec 中引导。现在应该会出现以下块。

[bin/rspec]

begin
  load File.expand_path("../spring", __FILE__)
rescue LoadError
end

关闭 Spring。运行 bin/rspec 并验证 Spring 是否被触发。

spring stop
spring status
bin/rspec
spring status

现在检查一下在 spring 为测试加载环境后测试是否运行得更快。

spring stop
time bin/rspec

[输出]

real    0m4.981s
user    0m0.144s
sys 0m0.032s

Spring 现在应该正在运行。让我们看看它是否在发挥作用。

time bin/rspec

[输出]

real    0m0.831s
user    0m0.140s
sys 0m0.034s

是的。

归根结底,如果您的 binstubs 已使用 Spring 引导,则调用 binstubs 将包括 Spring。当然,只有在 Spring 中注册过的命令才能使用 Spring,这也是之前 Gemfile 中包含 spring-commands-rspec 以支持 Rspec 的原因。

【讨论】:

  • 谢谢,我改成bin/rspec了。
  • 这里有同样的问题,更改为 bin/rspec 也对我有用。谢谢!
猜你喜欢
  • 1970-01-01
  • 2019-11-18
  • 2014-09-21
  • 2011-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多