ruby-on-rails - 使用 guard 时如何在 rspec 成功/失败时播放声音

标签 ruby-on-rails rspec guard

我用自动测试配置了一次,但最近我使用 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/

相关文章:

ruby-on-rails - 使用 session 变量设置数据库连接

ruby-on-rails - 为什么 Rails RSpec 响应显示 302 而不是 401?

ruby - cucumber 在不通过 guard 运行时抛出语法错误

ruby-on-rails - 是否可以将 Guard 与并行测试结合使用?

ruby - 如何禁用 Guard (Ruby) 中的通知?

ruby-on-rails - Minitest 问题 Ruby on Rails

ruby-on-rails - Rails 迁移 : primary key id with unsigned int(10)

ruby-on-rails - Rails 中使用数据库的动态角色授权。声明式授权是最佳选择吗?

ruby-on-rails - pundit rspec - 未初始化的常量 UserPolicy,为什么?

ruby-on-rails - 测试 :reject_if in anaf