spy 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,109 +1,115 @@
1
1
  require 'spec_helper'
2
+ require 'yaml'
3
+ require 'psych'
4
+ require 'syck'
2
5
 
3
- module RSpec
4
- module Mocks
5
- describe Serialization do
6
+ module Spy
7
+ describe "serialization" do
6
8
 
7
- class SerializableObject < Struct.new(:foo, :bar); end
9
+ class SerializableObject < Struct.new(:foo, :bar); end
8
10
 
9
- class SerializableMockProxy
10
- attr_reader :mock_proxy
11
+ class SerializableMockProxy
12
+ attr_reader :mock_proxy
11
13
 
12
- def initialize(mock_proxy)
13
- @mock_proxy = mock_proxy
14
- end
14
+ def initialize(mock_proxy)
15
+ @mock_proxy = mock_proxy
16
+ end
15
17
 
16
- def ==(other)
17
- other.class == self.class && other.mock_proxy == mock_proxy
18
- end
18
+ def ==(other)
19
+ other.class == self.class && other.mock_proxy == mock_proxy
19
20
  end
21
+ end
20
22
 
21
- def self.with_yaml_loaded(&block)
22
- context 'with YAML loaded' do
23
- module_eval(&block)
24
- end
23
+ def self.with_yaml_loaded(&block)
24
+ context 'with YAML loaded' do
25
+ module_eval(&block)
25
26
  end
27
+ end
26
28
 
27
- def self.without_yaml_loaded(&block)
28
- context 'without YAML loaded' do
29
- before do
30
- # We can't really unload yaml, but we can fake it here...
31
- hide_const("YAML")
32
- Struct.class_eval do
33
- alias __old_to_yaml to_yaml
34
- undef to_yaml
35
- end
29
+ def self.without_yaml_loaded(&block)
30
+ context 'without YAML loaded' do
31
+ before do
32
+ # We can't really unload yaml, but we can fake it here...
33
+ @yaml = YAML
34
+ Object.send(:remove_const, "YAML")
35
+ Struct.class_eval do
36
+ alias __old_to_yaml to_yaml
37
+ undef to_yaml
36
38
  end
39
+ end
37
40
 
38
- module_eval(&block)
41
+ module_eval(&block)
39
42
 
40
- after do
41
- Struct.class_eval do
42
- alias to_yaml __old_to_yaml
43
- undef __old_to_yaml
44
- end
43
+ after do
44
+ Struct.class_eval do
45
+ alias to_yaml __old_to_yaml
46
+ undef __old_to_yaml
45
47
  end
48
+
49
+ Object.send(:const_set, "YAML", @yaml)
46
50
  end
47
51
  end
52
+ end
48
53
 
49
- let(:serializable_object) { RSpec::Mocks::SerializableObject.new(7, "something") }
54
+ let(:serializable_object) { SerializableObject.new(7, "something") }
50
55
 
51
- def set_stub
52
- Spy.on(serializable_object, :bazz => 5)
53
- end
56
+ def set_spy
57
+ Spy::Subroutine.new(serializable_object, :bazz).hook(force: true).and_return(5)
58
+ end
54
59
 
55
- shared_examples_for 'normal YAML serialization' do
56
- it 'serializes to yaml the same with and without stubbing, using #to_yaml' do
57
- expect { set_stub }.to_not change { serializable_object.to_yaml }
58
- end
60
+ shared_examples_for 'normal YAML serialization' do
61
+ it 'serializes to yaml the same with and without stubbing, using #to_yaml' do
62
+ set_spy
63
+ expect { Spy.get(serializable_object, :bazz) }.to_not change { serializable_object.to_yaml }
64
+ end
59
65
 
60
- it 'serializes to yaml the same with and without stubbing, using YAML.dump' do
61
- expect { set_stub }.to_not change { ::YAML.dump(serializable_object) }
62
- end
66
+ it 'serializes to yaml the same with and without stubbing, using YAML.dump' do
67
+ set_spy
68
+ expect { Spy.get(serializable_object, :bazz) }.to_not change { ::YAML.dump(serializable_object) }
63
69
  end
70
+ end
64
71
 
65
- with_yaml_loaded do
66
- compiled_with_psych = begin
67
- require 'psych'
68
- true
69
- rescue LoadError
70
- false
72
+ with_yaml_loaded do
73
+ compiled_with_psych = begin
74
+ require 'psych'
75
+ true
76
+ rescue LoadError
77
+ false
78
+ end
79
+
80
+ if compiled_with_psych
81
+ context 'using Syck as the YAML engine' do
82
+ before(:each) { ::YAML::ENGINE.yamler = 'syck' }
83
+ it_behaves_like 'normal YAML serialization'
71
84
  end
72
85
 
73
- if compiled_with_psych
74
- context 'using Syck as the YAML engine' do
75
- before(:each) { ::YAML::ENGINE.yamler = 'syck' }
76
- it_behaves_like 'normal YAML serialization'
77
- end
78
-
79
- context 'using Psych as the YAML engine' do
80
- before(:each) { ::YAML::ENGINE.yamler = 'psych' }
81
- it_behaves_like 'normal YAML serialization'
82
- end
83
- else
86
+ context 'using Psych as the YAML engine' do
87
+ before(:each) { ::YAML::ENGINE.yamler = 'psych' }
84
88
  it_behaves_like 'normal YAML serialization'
85
89
  end
90
+ else
91
+ it_behaves_like 'normal YAML serialization'
86
92
  end
93
+ end
87
94
 
88
- without_yaml_loaded do
89
- it 'does not add #to_yaml to the stubbed object' do
90
- expect(serializable_object).not_to respond_to(:to_yaml)
91
- set_stub
92
- expect(serializable_object).not_to respond_to(:to_yaml)
93
- end
95
+ without_yaml_loaded do
96
+ it 'does not add #to_yaml to the stubbed object' do
97
+ expect(serializable_object).not_to respond_to(:to_yaml)
98
+ set_spy
99
+ expect(serializable_object).not_to respond_to(:to_yaml)
94
100
  end
101
+ end
95
102
 
96
- it 'marshals the same with and without stubbing' do
97
- expect { set_stub }.to_not change { Marshal.dump(serializable_object) }
98
- end
103
+ it 'marshals the same with and without stubbing' do
104
+ expect { Spy.get(serializable_object, :bazz) }.to_not change { Marshal.dump(serializable_object) }
105
+ end
99
106
 
100
- describe "an object that has its own mock_proxy instance variable" do
101
- let(:serializable_object) { RSpec::Mocks::SerializableMockProxy.new(:my_mock_proxy) }
107
+ describe "an object that has its own mock_proxy instance variable" do
108
+ let(:serializable_object) { SerializableMockProxy.new(:my_mock_proxy) }
102
109
 
103
- it 'does not interfere with its marshalling' do
104
- marshalled_copy = Marshal.load(Marshal.dump(serializable_object))
105
- expect(marshalled_copy).to eq serializable_object
106
- end
110
+ it 'does not interfere with its marshalling' do
111
+ marshalled_copy = Marshal.load(Marshal.dump(serializable_object))
112
+ expect(marshalled_copy).to eq serializable_object
107
113
  end
108
114
  end
109
115
  end
@@ -1,28 +1,31 @@
1
1
  require 'spec_helper'
2
2
 
3
- module RSpec
4
- module Mocks
3
+ module Spy
4
+ describe Subroutine do
5
+ class Bar
6
+ def foo(given = nil)
7
+ end
8
+ end
9
+
10
+ let(:obj) { Bar.new }
5
11
  describe "stub implementation" do
6
12
  describe "with no args" do
7
13
  it "execs the block when called" do
8
- obj = stub()
9
- Spy.stub(obj, :foo).and_return { :bar }
14
+ Subroutine.new(obj, :foo).hook.and_return { :bar }
10
15
  expect(obj.foo).to eq :bar
11
16
  end
12
17
  end
13
18
 
14
19
  describe "with one arg" do
15
20
  it "execs the block with that arg when called" do
16
- obj = stub()
17
- Spy.stub(obj, :foo).and_return {|given| given}
21
+ Subroutine.new(obj, :foo).hook.and_return {|given| given}
18
22
  expect(obj.foo(:bar)).to eq :bar
19
23
  end
20
24
  end
21
25
 
22
26
  describe "with variable args" do
23
27
  it "execs the block when called" do
24
- obj = stub()
25
- Spy.stub(obj, :foo).and_return {|*given| given.first}
28
+ Subroutine.new(obj, :foo).hook.and_return {|*given| given.first}
26
29
  expect(obj.foo(:bar)).to eq :bar
27
30
  end
28
31
  end
@@ -33,8 +36,7 @@ module RSpec
33
36
  it "replaces the stubbed method with the original method" do
34
37
  obj = Object.new
35
38
  def obj.foo; :original; end
36
- Spy.stub(obj, :foo)
37
- Spy.off(obj, :foo)
39
+ Subroutine.new(obj, :foo).hook.unhook
38
40
  expect(obj.foo).to eq :original
39
41
  end
40
42
 
@@ -42,8 +44,8 @@ module RSpec
42
44
  parent = Class.new
43
45
  child = Class.new(parent)
44
46
 
45
- Spy.stub(parent, :new)
46
- Spy.stub(child, :new)
47
+ Spy.on(parent, :new)
48
+ Spy.on(child, :new)
47
49
  Spy.off(parent, :new)
48
50
  Spy.off(child, :new)
49
51
 
@@ -29,55 +29,55 @@ module RSpec
29
29
  @stub = Object.new
30
30
  end
31
31
 
32
- describe "using Spy.stub" do
32
+ describe "using Spy::Subroutine.new" do
33
33
  it "returns declared value when message is received" do
34
- Spy.stub(@instance, :msg).and_return(:return_value)
34
+ Spy::Subroutine.new(@instance, :msg).hook(force: true).and_return(:return_value)
35
35
  expect(@instance.msg).to equal(:return_value)
36
36
  end
37
37
  end
38
38
 
39
39
  it "instructs an instance to respond_to the message" do
40
- Spy.stub(@instance, :msg)
40
+ Spy::Subroutine.new(@instance, :msg).hook(force: true)
41
41
  expect(@instance).to respond_to(:msg)
42
42
  end
43
43
 
44
44
  it "instructs a class object to respond_to the message" do
45
- Spy.stub(@class, :msg)
45
+ Spy::Subroutine.new(@class, :msg).hook(force: true)
46
46
  expect(@class).to respond_to(:msg)
47
47
  end
48
48
 
49
49
  it "handles multiple stubbed methods" do
50
- Spy.stub(@instance, :msg1 => 1, :msg2 => 2)
50
+ Spy::Subroutine.new(@instance, :msg1 => 1, :msg2 => 2).hook(force: true)
51
51
  expect(@instance.msg1).to eq(1)
52
52
  expect(@instance.msg2).to eq(2)
53
53
  end
54
54
 
55
55
  it "yields a specified object" do
56
- Spy.stub(@instance, :method_that_yields).and_yield(:yielded_obj)
56
+ Spy::Subroutine.new(@instance, :method_that_yields).hook(force: true).and_yield(:yielded_obj)
57
57
  current_value = :value_before
58
58
  @instance.method_that_yields {|val| current_value = val}
59
59
  expect(current_value).to eq :yielded_obj
60
60
  end
61
61
 
62
62
  it "yields a specified object and return another specified object" do
63
- yielded_obj = double("my mock")
64
- Spy.stub(yielded_obj, :foo)
65
- Spy.stub(@instance, :method_that_yields_and_returns).and_yield(yielded_obj).and_return(:baz)
63
+ yielded_obj = Spy.double("my mock")
64
+ Spy::Subroutine.new(yielded_obj, :foo).hook(force: true)
65
+ Spy::Subroutine.new(@instance, :method_that_yields_and_returns).hook(force: true).and_yield(yielded_obj).and_return(:baz)
66
66
  expect(@instance.method_that_yields_and_returns { |o| o.foo :bar }).to eq :baz
67
67
  end
68
68
 
69
69
  it "throws when told to" do
70
- Spy.stub(@stub, :something).and_throw(:up)
70
+ Spy::Subroutine.new(@stub, :something).hook(force: true).and_throw(:up)
71
71
  expect { @stub.something }.to throw_symbol(:up)
72
72
  end
73
73
 
74
74
  it "throws with argument when told to" do
75
- Spy.stub(@stub, :something).and_throw(:up, 'high')
75
+ Spy::Subroutine.new(@stub, :something).hook(force: true).and_throw(:up, 'high')
76
76
  expect { @stub.something }.to throw_symbol(:up, 'high')
77
77
  end
78
78
 
79
79
  it "overrides a pre-existing method" do
80
- Spy.stub(@stub, :existing_instance_method).and_return(:updated_stub_value)
80
+ Spy::Subroutine.new(@stub, :existing_instance_method).hook(force: true).and_return(:updated_stub_value)
81
81
  expect(@stub.existing_instance_method).to eq :updated_stub_value
82
82
  end
83
83
  end
@@ -1,57 +1,54 @@
1
1
  require 'spec_helper'
2
2
 
3
- module RSpec
4
- module Mocks
5
- describe TestDouble do
6
- before(:all) do
7
- Module.class_eval do
8
- private
9
- def use; end
10
- end
3
+ module Spy
4
+ describe Double do
5
+ before(:all) do
6
+ Module.class_eval do
7
+ private
8
+ def use; end
11
9
  end
10
+ end
12
11
 
13
- after(:all) do
14
- Module.class_eval do
15
- undef use
16
- end
12
+ after(:all) do
13
+ Module.class_eval do
14
+ undef use
17
15
  end
16
+ end
18
17
 
19
- it 'can be extended onto a module to make it a pure test double that can mock private methods' do
20
- double = Module.new
21
- Spy.on(double, :use)
22
- expect { double.use }.to raise_error(/private method `use' called/)
23
-
24
- double = Module.new { TestDouble.extend_onto(self) }
25
- double.should_receive(:use).and_return(:ok)
26
- expect(double.use).to be(:ok)
27
- end
18
+ it 'can be extended onto a module to make it a pure test double that can mock private methods' do
19
+ double = Module.new
20
+ Spy.on(double, :use)
21
+ expect { double.use }.to raise_error(/private method `use' called/)
28
22
 
29
- it 'sets the test double name when a name is passed' do
30
- double = Module.new { TestDouble.extend_onto(self, "MyDouble") }
31
- expect { double.foo }.to raise_error(/Mock "MyDouble" received/)
32
- end
23
+ double = Module.new { TestDouble.extend_onto(self) }
24
+ double.should_receive(:use).and_return(:ok)
25
+ expect(double.use).to be(:ok)
26
+ end
33
27
 
34
- it 'stubs the methods passed in the stubs hash' do
35
- double = Module.new do
36
- TestDouble.extend_onto(self, "MyDouble", :a => 5, :b => 10)
37
- end
28
+ it 'sets the test double name when a name is passed' do
29
+ double = Module.new { TestDouble.extend_onto(self, "MyDouble") }
30
+ expect { double.foo }.to raise_error(/Mock "MyDouble" received/)
31
+ end
38
32
 
39
- expect(double.a).to eq(5)
40
- expect(double.b).to eq(10)
33
+ it 'stubs the methods passed in the stubs hash' do
34
+ double = Module.new do
35
+ TestDouble.extend_onto(self, "MyDouble", :a => 5, :b => 10)
41
36
  end
42
37
 
43
- it 'indicates what type of test double it is in error messages' do
44
- double = Module.new do
45
- TestDouble.extend_onto(self, "A", :__declared_as => "ModuleMock")
46
- end
47
- expect { double.foo }.to raise_error(/ModuleMock "A"/)
48
- end
38
+ expect(double.a).to eq(5)
39
+ expect(double.b).to eq(10)
40
+ end
49
41
 
50
- it 'is declared as a mock by default' do
51
- double = Module.new { TestDouble.extend_onto(self) }
52
- expect { double.foo }.to raise_error(/Mock received/)
42
+ it 'indicates what type of test double it is in error messages' do
43
+ double = Module.new do
44
+ TestDouble.extend_onto(self, "A", :__declared_as => "ModuleMock")
53
45
  end
46
+ expect { double.foo }.to raise_error(/ModuleMock "A"/)
47
+ end
48
+
49
+ it 'is declared as a mock by default' do
50
+ double = Module.new { TestDouble.extend_onto(self) }
51
+ expect { double.foo }.to raise_error(/Mock received/)
54
52
  end
55
53
  end
56
54
  end
57
-
@@ -17,5 +17,6 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
  gem.add_development_dependency('minitest', '>= 4.5.0')
20
- gem.add_development_dependency('rspec')
20
+ gem.add_development_dependency('rspec-core')
21
+ gem.add_development_dependency('rspec-expectations')
21
22
  end
@@ -0,0 +1,58 @@
1
+ require 'test_helper'
2
+
3
+ class TestConstantSpying < MiniTest::Unit::TestCase
4
+
5
+ class Foo
6
+ HELLO = "hello world"
7
+
8
+ def self.hello
9
+ HELLO
10
+ end
11
+
12
+ module Bar
13
+ def self.hello
14
+ HELLO
15
+ end
16
+ end
17
+ end
18
+
19
+ class ChildFoo < Foo
20
+ def self.hello
21
+ HELLO
22
+ end
23
+ end
24
+
25
+ def setup
26
+ Spy::Agency.instance.dissolve!
27
+ end
28
+
29
+ def test_spy_on_constant
30
+ assert_equal "hello world", Foo.hello
31
+
32
+ spy = Spy.on_const(Foo, :HELLO)
33
+ assert_equal nil, Foo.hello
34
+ spy.and_return("awesome")
35
+ assert_equal "awesome", Foo.hello
36
+
37
+ Spy.off_const(Foo, :HELLO)
38
+ assert_equal "hello world", Foo.hello
39
+
40
+ assert_equal "hello world", Foo::Bar.hello
41
+ spy = Spy.on_const(Foo, :HELLO)
42
+ assert_equal nil, Foo::Bar.hello
43
+ spy.and_return("awesome")
44
+ assert_equal "awesome", Foo::Bar.hello
45
+
46
+ Spy.off_const(Foo, :HELLO)
47
+ assert_equal "hello world", Foo::Bar.hello
48
+
49
+ assert_equal "hello world", ChildFoo.hello
50
+ spy = Spy.on_const(Foo, :HELLO)
51
+ assert_equal nil, ChildFoo.hello
52
+ spy.and_return("awesome")
53
+ assert_equal "awesome", ChildFoo.hello
54
+
55
+ Spy.off_const(Foo, :HELLO)
56
+ assert_equal "hello world", ChildFoo.hello
57
+ end
58
+ end