我用自动测试配置了一次,但最近我使用 guard-rspec 在后台运行我的规范。我确实有咆哮通知,但这需要阅读实际的通知文本,这在快速的红绿循环中会分散注意力。我更喜欢成功和失败的声音通知,但我找不到任何此类设置的现成示例。
最佳答案
我也没有看到这样的示例设置,所以你需要实现一个 Notifier :
module Guard::Notifier::Sound
extend self
def available?(silent = false, options = {})
true
end
def notify(type, title, message, image, options = { })
puts 'Play sound: ', type
end
end
您可以将此代码直接放入您的Guardfile
中,通过以下代码注册并使用它:
Guard::Notifier::NOTIFIERS << [[:sound, ::Guard::Notifier::Sound]]
notification :sound
当然你需要实现实际的声音播放。一个简单的实现是 fork 给外部播放器,例如:
def notify(type, title, message, image, options = { })
fork{ exec 'mpg123','-q',"spec/support/sound/#{ type }.mp3" }
end
更新
对于 Spork,上述直接包含到 Guardfile
中的方法将不起作用,因为 Spork 在单独的进程中运行并且不会对其求值。您需要创建一个支持文件,例如spec/support/sound_notifier.rb
内容如下:
module Guard::Notifier::Sound
extend self
def available?(silent = false, options = {})
true
end
def notify(type, title, message, image, options = { })
fork{ exec 'mpg123','-q',"spec/support/sound/#{ type }.mp3" }
end
end
Guard::Notifier::NOTIFIERS << [[:sound, ::Guard::Notifier::Sound]]
刚刚
require 'spec/support/sound_notifier'
notification :sound
在 Guardfile
中。接下来,您还需要在 Spork 进程中加载 sound_notifier
。因为我不使用 Spork 我无法验证它,但是当我记得正确时发生在 spec_helper.rb
中:
Spork.prefork do
require 'spec/support/sound_notifier'
end
关于ruby-on-rails - 使用 guard 时如何在 rspec 成功/失败时播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17021997/