使用后卫时,如何在rspec成功/失败时播放声音

时间:2013-06-10 10:43:05

标签: ruby-on-rails rspec guard

我用autotest配置了一次,但最近我使用guard-rspec在后台运行我的规格。我确实有咆哮通知,但这需要阅读实际的通知文本,这在快速红绿循环期间会分散注意力。我希望成功和失败的声音通知,但我找不到任何这种设置的现成例子。

1 个答案:

答案 0 :(得分:1)

我还没有看到这样的示例设置,因此您需要实现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

当然你需要实现真正的声音播放。一个简单的实现是分叉到外部播放器,如:

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