expects_chain 0.0.1

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.
@@ -0,0 +1,38 @@
1
+ module ExpectsChain
2
+ class Expectation
3
+ def initialize type, object, *args
4
+ @type = type
5
+ @object = object
6
+ @expectations = args
7
+ end
8
+
9
+ def returns value
10
+ set_up_call_chain value
11
+ value
12
+ end
13
+
14
+ private
15
+
16
+ def set_up_call_chain value
17
+ current_mock = value
18
+ first_expectation = @expectations.shift
19
+ @expectations.reverse.each do |expectation|
20
+ old_mock, current_mock = current_mock, mocker.send(@type)
21
+ set_expectation(current_mock, expectation, old_mock)
22
+ end
23
+ set_expectation(@object, first_expectation, current_mock)
24
+ end
25
+
26
+ def set_expectation obj, method, ret
27
+ if method.kind_of?(Array) && method.first.kind_of?(Symbol)
28
+ mocker.send("#{@type}_object_with_arguments".to_sym, obj, method.shift, method, ret)
29
+ else
30
+ mocker.send("#{@type}_object".to_sym, obj, method, ret)
31
+ end
32
+ end
33
+
34
+ def mocker
35
+ @_mocker ||= Mockers::Mocha
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,29 @@
1
+ module ExpectsChain
2
+ module Mockers
3
+ module Mocha
4
+ def self.mock_object obj, method, ret
5
+ obj.expects(method).at_least_once.returns(ret)
6
+ end
7
+
8
+ def self.mock_object_with_arguments obj, method, attrs, ret
9
+ obj.expects(method).with(*attrs).at_least_once.returns(ret)
10
+ end
11
+
12
+ def self.stub_object
13
+ obj.stubs(method).returns(ret)
14
+ end
15
+
16
+ def self.stub_object_with_arguments obj, method, attrs, ret
17
+ obj.stubs(method).with(*attrs).at_least_once.returns(ret)
18
+ end
19
+
20
+ def self.mock
21
+ ::Mocha::Mock.new(nil)
22
+ end
23
+
24
+ def self.stub
25
+ ::Mocha::Mock.new(nil)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ require 'expects_chain/expectation'
4
+ require 'expects_chain/mockers/mocha'
5
+
6
+ module ExpectsChain
7
+ module Methods
8
+ def expects_chain *args
9
+ ExpectsChain::Expectation.new(:mock, self, *args)
10
+ end
11
+
12
+ def stubs_chain *args
13
+ ExpectsChain::Expectation.new(:stub, self, *args)
14
+ end
15
+ end
16
+ end
17
+
18
+ class Object
19
+ include ExpectsChain::Methods
20
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: expects_chain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tomasz Pewiński
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-21 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: RSpec extension that allows you to set chained expectations on objects.
15
+ Currently supports mocha
16
+ email: pewniak747@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/expects_chain.rb
22
+ - lib/expects_chain/expectation.rb
23
+ - lib/expects_chain/mockers/mocha.rb
24
+ homepage: http://github.com/pewniak747/expects_chain
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.11
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: RSpec extension that allows you to set chained expectations on objects.
48
+ test_files: []