mock_factory 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/mock_factory.rb CHANGED
@@ -1,31 +1,46 @@
1
- require 'rails'
2
1
  require 'rspec/core'
3
2
  require 'rspec/mocks'
4
3
  require 'rspec/rails/mocks'
5
4
 
6
5
  class MockFactory
7
6
 
8
- VERSION = '0.0.3'
7
+ VERSION = '0.0.4'
9
8
 
10
9
  class << self
11
10
  include RSpec::Rails::Mocks
12
11
 
13
- def create(const)
14
- name = const.is_a?(String) ? const : const.name
12
+ def create(const, &block)
13
+ mock_it 'create', const, &block
14
+ end
15
+
16
+
17
+ def fetch(const, &block)
18
+ mock_it 'fetch', const, &block
19
+ end
20
+
21
+ private
22
+
23
+ def mock_it(action, const, &block)
15
24
  setup
16
- return @mocks[name] if @mocks[name]
17
- @mocks[name] = mock_model name
18
- yield @mocks[name] if block_given?
19
- @mocks[name]
25
+ name = const.is_a?(String) ? const : const.name
26
+
27
+ mock_object = if action == 'fetch'
28
+ @mocks[name] ||= mock_model name
29
+ else
30
+ mock_model name
31
+ end
32
+
33
+ block.call(mock_object) if block
34
+ mock_object
20
35
  end
21
36
 
22
- protected
23
37
 
24
38
  def setup
25
39
  @mocks ||= {}
26
40
  @space ||= RSpec::Mocks::setup(self)
27
41
  end
28
42
 
43
+
29
44
  end
30
45
 
31
46
  end
@@ -3,14 +3,13 @@ require 'mock_factory'
3
3
 
4
4
  describe MockFactory do
5
5
 
6
- it "makes a mock object" do
7
- defined?(Blark).should be_nil
6
+ it "create makes a mock object" do
8
7
  o = MockFactory.create 'Blark'
9
8
  o.should be_a(Blark)
10
9
  end
11
10
 
12
11
 
13
- it "applies a block to the mock" do
12
+ it "create applies a block to the mock" do
14
13
  b = MockFactory.create 'Badger' do |b|
15
14
  b.stub(:honey).and_return(true)
16
15
  end
@@ -18,9 +17,30 @@ describe MockFactory do
18
17
  end
19
18
 
20
19
 
21
- it "makes the same object" do
20
+ it "create makes a different object" do
22
21
  stout = MockFactory.create 'Beer'
23
22
  ipa = MockFactory.create 'Beer'
23
+ stout.should_not === ipa
24
+ end
25
+
26
+
27
+ it "fetch makes a mock object" do
28
+ o = MockFactory.fetch 'Blark'
29
+ o.should be_a(Blark)
30
+ end
31
+
32
+
33
+ it "fetch applies a block to the mock" do
34
+ b = MockFactory.fetch 'Badger' do |b|
35
+ b.stub(:honey).and_return(true)
36
+ end
37
+ b.honey.should be_true
38
+ end
39
+
40
+
41
+ it "fetch makes the same object" do
42
+ stout = MockFactory.fetch 'Beer'
43
+ ipa = MockFactory.fetch 'Beer'
24
44
  stout.should === ipa
25
45
  end
26
46
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mock_factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -115,7 +115,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
115
  version: '0'
116
116
  segments:
117
117
  - 0
118
- hash: 797332883
118
+ hash: 551829121
119
119
  required_rubygems_version: !ruby/object:Gem::Requirement
120
120
  none: false
121
121
  requirements:
@@ -124,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
124
  version: '0'
125
125
  segments:
126
126
  - 0
127
- hash: 797332883
127
+ hash: 551829121
128
128
  requirements: []
129
129
  rubyforge_project: mock_factory
130
130
  rubygems_version: 1.8.24