Skip to content

Commit

Permalink
get specs working with mock verification
Browse files Browse the repository at this point in the history
  • Loading branch information
e2 committed Jun 11, 2014
1 parent c73827c commit 32030df
Show file tree
Hide file tree
Showing 19 changed files with 183 additions and 85 deletions.
2 changes: 1 addition & 1 deletion lib/guard/notifier.rb
Expand Up @@ -92,7 +92,7 @@ def clear_notifiers
# @option options [Boolean] silent disable any logging
#
def turn_on(opts = {})
if notifiers.empty? && (!::Guard.options || ::Guard.options[:notify])
if notifiers.empty? && (::Guard.options || {})[:notify]
_auto_detect_notification
end

Expand Down
2 changes: 1 addition & 1 deletion lib/guard/plugin_util.rb
Expand Up @@ -37,7 +37,7 @@ def self.plugin_names
end
end
else
Guard::UI.deprecation \
::Guard::UI.deprecation \
'Rubygems version prior to 1.8.0 are no longer supported'\
' and may not work'

Expand Down
8 changes: 6 additions & 2 deletions spec/guard_spec.rb
Expand Up @@ -171,6 +171,7 @@
describe '.groups' do
subject do
allow(Guard::Notifier).to receive(:turn_on)
allow(Listen).to receive(:to).with(Dir.pwd, {})
guard = ::Guard.setup
@group_backend = guard.add_group(:backend)
@group_backflip = guard.add_group(:backflip)
Expand Down Expand Up @@ -217,7 +218,10 @@
describe '.group' do
subject do
allow(Guard::Notifier).to receive(:turn_on)
allow(Listen).to receive(:to).with(Dir.pwd, {})

guard = ::Guard.setup

@group_backend = guard.add_group(:backend)
@group_backflip = guard.add_group(:backflip)
guard
Expand Down Expand Up @@ -255,8 +259,8 @@
end

describe '.add_plugin' do
let(:plugin_util) { double('Guard::PluginUtil') }
let(:guard_rspec) { double('Guard::RSpec instance') }
let(:plugin_util) { instance_double(Guard::PluginUtil) }
let(:guard_rspec) { instance_double(Guard::RSpec) }

before do
expect(::Guard::PluginUtil).to receive(:new).with('rspec') { plugin_util }
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/guard/cli_spec.rb
Expand Up @@ -4,7 +4,7 @@
describe Guard::CLI do
let(:guard) { Guard }
let(:ui) { Guard::UI }
let(:dsl_describer) { double('DslDescriber instance') }
let(:dsl_describer) { instance_double(DslDescriber) }

describe '#start' do
before { allow(Guard).to receive(:start) }
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/guard/commander_spec.rb
Expand Up @@ -66,6 +66,7 @@
it 'turns the notifier off' do
expect(::Guard::Notifier).to receive(:turn_off)

allow(Guard::Sheller).to receive(:run).with(*%w(hash stty)) { false }
::Guard.stop
end

Expand All @@ -83,7 +84,7 @@
end

describe '.reload' do
let(:runner) { double(run: true) }
let(:runner) { instance_double(Guard::Runner, run: true) }
let(:group) { ::Guard::Group.new('frontend') }
subject { ::Guard.setup }

Expand Down Expand Up @@ -134,14 +135,13 @@
end

describe '.run_all' do
let(:runner) { double(run: true) }
let(:runner) { instance_double(Guard::Runner, run: true) }
let(:group) { ::Guard::Group.new('frontend') }
subject { ::Guard.setup }

before do
allow(::Guard::Notifier).to receive(:turn_on)
allow(::Guard).to receive(:runner) { runner }
allow(::Guard).to receive(:within_preserved_state).and_yield
allow(::Guard::UI).to receive(:action_with_scopes)
allow(::Guard::UI).to receive(:clear)
allow(Listen).to receive(:to).with(Dir.pwd, {})
Expand Down
13 changes: 11 additions & 2 deletions spec/lib/guard/deprecated_methods_spec.rb
Expand Up @@ -6,6 +6,12 @@
before(:all) do
module TestModule
extend DeprecatedMethods

def self.plugins(_filter)
end

def self.add_plugin(*_args)
end
end
end

Expand Down Expand Up @@ -43,7 +49,7 @@ module TestModule
end

describe '.get_guard_class' do
let(:plugin_util) { double('Guard::PluginUtil', plugin_class: true) }
let(:plugin_util) { instance_double(Guard::PluginUtil, plugin_class: true) }
before { allow(PluginUtil).to receive(:new).and_return(plugin_util) }

it 'displays a deprecation warning to the user' do
Expand Down Expand Up @@ -72,7 +78,10 @@ module TestModule
end

describe '.locate_guard' do
let(:plugin_util) { double('Guard::PluginUtil', plugin_location: true) }
let(:plugin_util) do
instance_double(Guard::PluginUtil, plugin_location: true)
end

before do
allow(PluginUtil).to receive(:new) { plugin_util }
end
Expand Down
2 changes: 2 additions & 0 deletions spec/lib/guard/dsl_describer_spec.rb
@@ -1,6 +1,7 @@
# encoding: utf-8
require 'spec_helper'
require 'guard/plugin'
require 'guard/dsl_describer'
require 'formatador'

describe Guard::DslDescriber do
Expand Down Expand Up @@ -31,6 +32,7 @@

before do
allow(Guard::Notifier).to receive(:turn_on)
allow(Listen).to receive(:to).with(Dir.pwd, {})
Guard.setup

stub_const 'Guard::Test', Class.new(Guard::Plugin)
Expand Down
15 changes: 8 additions & 7 deletions spec/lib/guard/dsl_spec.rb
Expand Up @@ -6,13 +6,14 @@
let(:local_guardfile) { File.join(Dir.pwd, 'Guardfile') }
let(:home_guardfile) { File.expand_path(File.join('~', '.Guardfile')) }
let(:home_config) { File.expand_path(File.join('~', '.guard.rb')) }
let(:guardfile_evaluator) { double('Guard::Guardfile::Evaluator') }
let(:guardfile_evaluator) { instance_double(Guard::Guardfile::Evaluator) }

before do
stub_const 'Guard::Foo', Class.new(Guard::Plugin)
stub_const 'Guard::Bar', Class.new(Guard::Plugin)
stub_const 'Guard::Baz', Class.new(Guard::Plugin)
allow(::Guard).to receive(:setup_interactor)
allow(Guard::Notifier).to receive(:turn_on)
allow(Listen).to receive(:to).with(Dir.pwd, {})
::Guard.setup
end

Expand Down Expand Up @@ -40,7 +41,7 @@ def self.disable_user_config

describe '#ignore' do
disable_user_config
let(:listener) { double }
let(:listener) { instance_double(Listen::Listener) }

it 'adds ignored regexps to the listener' do
allow(::Guard).to receive(:listener) { listener }
Expand All @@ -54,7 +55,7 @@ def self.disable_user_config

describe '#ignore!' do
disable_user_config
let(:listener) { double }
let(:listener) { instance_double(Listen::Listener) }

context 'when ignoring only foo* and *bar*' do
let(:contents) { 'ignore! %r{^foo}, /bar/' }
Expand Down Expand Up @@ -83,7 +84,7 @@ def self.disable_user_config

describe '#filter' do
disable_user_config
let(:listener) { double }
let(:listener) { instance_double(Listen::Listener) }

it 'adds ignored regexps to the listener' do
allow(::Guard).to receive(:listener) { listener }
Expand All @@ -97,7 +98,7 @@ def self.disable_user_config

describe '#filter!' do
disable_user_config
let(:listener) { double }
let(:listener) { instance_double(Listen::Listener) }

it 'replaces ignored regexps in the listener' do
allow(::Guard).to receive(:listener) { listener }
Expand Down Expand Up @@ -150,7 +151,7 @@ def self.disable_user_config
it 'disables the interactions with :off' do
expected = 'interactor :off'
described_class.evaluate_guardfile(guardfile_contents: expected)
expect(Guard::Interactor.enabled).to be_falsey
expect(Guard::Interactor).to_not be_enabled
end

it 'passes the options to the interactor' do
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/guard/guardfile/evaluator_spec.rb
Expand Up @@ -10,7 +10,9 @@
let(:rel_guardfile) { File.expand_path('../relative_path_to_Guardfile') }

before do
allow(::Guard).to receive(:setup_interactor)
allow(::Guard::Interactor).to receive(:new).with(false)
allow(Listen).to receive(:to).with(Dir.pwd, {})

allow(Guard::Notifier).to receive(:turn_on)
::Guard.setup
allow(Guard::Notifier).to receive(:notify)
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/guard/guardfile/generator_spec.rb
Expand Up @@ -2,7 +2,7 @@

describe Guard::Guardfile::Generator do

let(:plugin_util) { double('Guard::PluginUtil') }
let(:plugin_util) { instance_double(Guard::PluginUtil) }
let(:guardfile_generator) { described_class.new }

it 'has a valid Guardfile template' do
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/guard/guardfile_spec.rb
Expand Up @@ -4,7 +4,7 @@

describe Guardfile do

let(:guardfile_generator) { double('Guard::Guardfile::Generator') }
let(:guardfile_generator) { instance_double(Guard::Guardfile::Generator) }

describe '.create_guardfile' do
it 'displays a deprecation warning to the user' do
Expand Down
10 changes: 5 additions & 5 deletions spec/lib/guard/interactor_spec.rb
Expand Up @@ -105,6 +105,7 @@

::Guard.interactor.background
end

after do
::Guard.interactor.background
ENV['GUARD_ENV'] = 'test'
Expand Down Expand Up @@ -139,12 +140,11 @@
allow(::Guard.scope).to receive(:[]).with(:plugins).and_return([])
allow(::Guard.scope).to receive(:[]).with(:groups).and_return([])

allow(::Guard).to receive(:listener).
and_return(double('listener', paused?: false))
allow(listener).to receive(:paused?).and_return(false)

expect(::Guard.interactor).to receive(:_clip_name).and_return('main')
end
let(:pry) { double(input_array: []) }
let(:pry) { instance_double(Pry, input_array: []) }

context 'Guard is not paused' do
it 'displays "guard"' do
Expand All @@ -155,8 +155,7 @@

context 'Guard is paused' do
before do
allow(::Guard).to receive(:listener).
and_return(double('listener', paused?: true))
allow(listener).to receive(:paused?).and_return(true)
end

it 'displays "pause"' do
Expand All @@ -179,6 +178,7 @@
end

context 'with a plugins scope' do

before do
allow(::Guard.scope).to receive(:[]).with(:plugins).
and_return([double(title: 'RSpec'), double(title: 'Ronn')])
Expand Down
18 changes: 8 additions & 10 deletions spec/lib/guard/notifier_spec.rb
Expand Up @@ -32,8 +32,6 @@
end

it 'turns on the defined notification module' do
expect(Guard::Notifier::GNTP).to receive(:turn_on)

Guard::Notifier.turn_on
end
end
Expand All @@ -45,7 +43,8 @@

context 'when notifications are globally enabled' do
before do
expect(::Guard.options).to receive(:[]).with(:notify).and_return true
# called 3 times - twice from spec_helper to turn off debugging
allow(::Guard).to receive(:options).and_return(notify: true)
end

it 'tries to add each available notification silently' do
Expand Down Expand Up @@ -150,15 +149,14 @@

context 'when notifications are globally disabled' do
before do
expect(::Guard.options).to receive(:[]).
with(:notify).and_return false

# Called 3 times, 2 times from spec_helper to unset debug
allow(::Guard).to receive(:options).and_return(notify: false)
end

it 'does not try to add each available notification silently' do
expect(Guard::Notifier).to_not receive(:auto_detect_notification)
expect(Guard::Notifier).to_not receive(:_auto_detect_notification)
Guard::Notifier.turn_on
expect(Guard::Notifier).not_to be_enabled
expect(Guard::Notifier).to_not be_enabled
end
end
end
Expand Down Expand Up @@ -298,8 +296,8 @@
end

it 'does not send any notifications to a notifier' do
expect(gntp).to_not receive(:notify)
expect(growl).to_not receive(:notify)
expect(gntp_object).to_not receive(:notify)
expect(growl_object).to_not receive(:notify)

::Guard::Notifier.notify('Hi to everyone')
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/guard/plugin/hooker_spec.rb
Expand Up @@ -3,8 +3,6 @@

describe Guard::Plugin::Hooker do

let(:listener) { double('listener').as_null_object }

let(:fake_plugin) do
Class.new(Guard::Plugin) do
def start
Expand All @@ -29,6 +27,8 @@ def run_on_modifications(_paths)
let(:dummy1) { fake_plugin.new }
let(:dummy2) { fake_plugin.new }

let(:listener) { instance_double(Proc, call: nil) }

before do
stub_const 'Guard::Dummy', fake_plugin
described_class.add_callback(listener, dummy1, :start_begin)
Expand Down
18 changes: 7 additions & 11 deletions spec/lib/guard/plugin_util_spec.rb
@@ -1,23 +1,19 @@
require 'spec_helper'

require 'guard/plugin'

# Needed for a test below
module Guard
class Guard
end
end
require 'guard/guard'

describe Guard::PluginUtil do

let!(:rubygems_version_1_7_2) { Gem::Version.create('1.7.2') }
let!(:rubygems_version_1_8_0) { Gem::Version.create('1.8.0') }
let(:guard_rspec_class) { double('Guard::RSpec') }
let(:guard_rspec) { double('Guard::RSpec instance') }
let(:guardfile_evaluator) { double('Guard::Guardfile::Evaluator instance') }
let(:guard_rspec_class) { class_double(Guard::RSpec) }
let(:guard_rspec) { instance_double(Guard::RSpec) }
let(:guardfile_evaluator) { instance_double(Guard::Guardfile::Evaluator) }

before do
allow(Notifier).to receive(:turn_on) {}
allow(Listen).to receive(:to).with(Dir.pwd, {})
allow(Guard::Notifier).to receive(:turn_on) {}
Guard.setup
end

Expand Down Expand Up @@ -271,7 +267,7 @@ class VSpec
it 'returns the Guard class' do
plugin = described_class.new('inline')
module Guard
class Inline < Guard
class Inline < ::Guard::Plugin
end
end

Expand Down

0 comments on commit 32030df

Please sign in to comment.