gpt4 book ai didi

ruby-on-rails - Guard-RSpec 和 Spring : command `cmd: spring rspec` doesn't run the specs within Guard

转载 作者:行者123 更新时间:2023-12-01 22:36:30 25 4
gpt4 key购买 nike

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 结果 - 失败”之类的通知。

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

guard :rspec, cmd: 'rspec' do

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

我还必须提到,当从 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

最佳答案

我之前一直在研究这个问题。

Binstubs are wrapper scripts around executables. In Rails they live inside bin/. If you run spring binstub --all, your binstubs will be using Spring.

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

一个小测试来验证。确保您已经对 rspec 进行了 binstubbed。

bundle binstubs 'rspec-core'

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

[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。

[ 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 中引导。现在应该出现以下 block 。

[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,这就是为什么早先将 spring-commands-rspec 包含在 Gemfile 中以支持 Rspec 的原因。

关于ruby-on-rails - Guard-RSpec 和 Spring : command `cmd: spring rspec` doesn't run the specs within Guard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22327463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com