expectations 0.2.1 → 0.2.3
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/README +33 -3
- data/lib/expectations/delegate_recorder.rb +30 -12
- data/lib/expectations/expectation.rb +3 -6
- data/lib/expectations/mock_recorder.rb +15 -24
- data/lib/expectations/object.rb +3 -11
- data/lib/expectations/{recorded_state_based_expectation.rb → recorded_expectation.rb} +7 -4
- data/lib/expectations/recorder.rb +44 -0
- data/lib/expectations/reverse_result.rb +7 -0
- data/lib/expectations/state_based_recorder.rb +13 -16
- data/lib/expectations/suite.rb +2 -5
- data/lib/expectations.rb +3 -5
- data/rakefile.rb +1 -1
- data/test/expectations/expectation_test.rb +6 -22
- data/test/expectations/expectations_test.rb +2 -2
- data/test/expectations/suite_runner_test.rb +2 -2
- data/test/expectations/suite_test.rb +25 -5
- data/test/failures_test.rb +5 -3
- data/test/successes_test.rb +33 -3
- metadata +5 -7
- data/lib/expectations/behavior_based_expectation.rb +0 -20
- data/lib/expectations/delegate_expectation.rb +0 -24
- data/lib/expectations/negative_state_based_recorder.rb +0 -11
- data/lib/expectations/positive_state_based_recorder.rb +0 -15
data/README
CHANGED
@@ -57,13 +57,25 @@ expectations can be used for state based and behavior based testing.
|
|
57
57
|
end
|
58
58
|
|
59
59
|
# Behavior based test using a traditional mock
|
60
|
-
expect mock.
|
60
|
+
expect mock.to.receive(:dial).with("2125551212").times(2) do |phone|
|
61
|
+
phone.dial("2125551212")
|
62
|
+
phone.dial("2125551212")
|
63
|
+
end
|
64
|
+
|
65
|
+
# Behavior based test using a stub
|
66
|
+
expect stub.to.receive(:dial).with("2125551212").times(2) do |phone|
|
67
|
+
phone.dial("2125551212")
|
68
|
+
phone.dial("2125551212")
|
69
|
+
end
|
70
|
+
|
71
|
+
# Behavior based test using a stub_everything
|
72
|
+
expect stub_everything.to.receive(:dial).with("2125551212").times(2) do |phone|
|
61
73
|
phone.dial("2125551212")
|
62
74
|
phone.dial("2125551212")
|
63
75
|
end
|
64
76
|
|
65
77
|
# Behavior based test on a concrete mock
|
66
|
-
expect Object.
|
78
|
+
expect Object.to.receive(:deal) do
|
67
79
|
Object.deal
|
68
80
|
end
|
69
81
|
|
@@ -140,10 +152,28 @@ expectations can be used for state based and behavior based testing.
|
|
140
152
|
end
|
141
153
|
end
|
142
154
|
# State based delegation test
|
143
|
-
expect klass.new.
|
155
|
+
expect klass.new.to.delegate(:save).to(:record) do |instance|
|
144
156
|
instance.save(1)
|
145
157
|
end
|
146
158
|
|
159
|
+
# this is normally defined in the file specific to the class
|
160
|
+
klass = Class.new do
|
161
|
+
attr_accessor :started
|
162
|
+
end
|
163
|
+
# State based fluent interface boolean test using to be
|
164
|
+
expect klass.new.to.be.started do |process|
|
165
|
+
process.started = true
|
166
|
+
end
|
167
|
+
|
168
|
+
# this is normally defined in the file specific to the class
|
169
|
+
klass = Class.new do
|
170
|
+
attr_accessor :finished
|
171
|
+
end
|
172
|
+
# State based fluent interface boolean test using to have
|
173
|
+
expect klass.new.to.have.finished do |process|
|
174
|
+
process.finished = true
|
175
|
+
end
|
176
|
+
|
147
177
|
end
|
148
178
|
|
149
179
|
== Contributors
|
@@ -1,22 +1,40 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
@subject_mock.
|
1
|
+
module Expectations::DelegateRecorder
|
2
|
+
attr_accessor :delegation_result
|
3
|
+
|
4
|
+
def delegate!(meth)
|
5
|
+
@subject_mock = Mocha::Mock.new
|
6
|
+
@meth = meth
|
7
|
+
@subject_mock.expects(meth).returns(:a_delegated_return_value)
|
8
|
+
recorder = self
|
9
|
+
mod = Module.new do
|
10
|
+
define_method meth do |*args|
|
11
|
+
recorder.delegation_result = super
|
12
|
+
end
|
13
|
+
end
|
14
|
+
subject.extend mod
|
6
15
|
end
|
7
16
|
|
8
|
-
def to(
|
9
|
-
@
|
17
|
+
def to(receiver)
|
18
|
+
@receiver = receiver
|
10
19
|
self
|
11
20
|
end
|
12
21
|
|
13
|
-
def
|
14
|
-
|
15
|
-
|
22
|
+
def subject!
|
23
|
+
subject.stubs(@receiver).returns(@subject_mock)
|
24
|
+
subject
|
16
25
|
end
|
17
26
|
|
18
|
-
def verify
|
27
|
+
def verify
|
19
28
|
@subject_mock.verify
|
20
|
-
|
29
|
+
:a_delegated_return_value == delegation_result
|
30
|
+
end
|
31
|
+
|
32
|
+
def failure_message
|
33
|
+
"expected #{subject}.#{@meth} to return the value of #{subject}.#{@receiver}.#{@meth}; however, #{subject}.#{@meth} returned #{delegation_result}"
|
21
34
|
end
|
35
|
+
|
36
|
+
def mocha_error_message(ex)
|
37
|
+
"expected #{subject} to delegate #{@meth} to #{@receiver}; however, #{subject}.#{@meth} was never called"
|
38
|
+
end
|
39
|
+
|
22
40
|
end
|
@@ -2,14 +2,11 @@ class Expectations::Expectation
|
|
2
2
|
include Mocha::Standalone
|
3
3
|
attr_accessor :expected, :block, :file, :line, :actual
|
4
4
|
|
5
|
-
def initialize(expected, &block)
|
5
|
+
def initialize(expected, file, line, &block)
|
6
6
|
self.expected, self.block = expected, block
|
7
|
-
self.file, self.line =
|
7
|
+
self.file, self.line = file, line.to_i
|
8
8
|
case
|
9
|
-
when expected.is_a?(Expectations::
|
10
|
-
when expected.is_a?(Expectations::PositiveStateBasedRecorder) then extend(Expectations::RecordedStateBasedExpectation)
|
11
|
-
when expected.is_a?(Expectations::NegativeStateBasedRecorder) then extend(Expectations::RecordedStateBasedExpectation)
|
12
|
-
when expected.is_a?(Expectations::MockRecorder) then extend(Expectations::BehaviorBasedExpectation)
|
9
|
+
when expected.is_a?(Expectations::Recorder) then extend(Expectations::RecordedExpectation)
|
13
10
|
else extend(Expectations::StateBasedExpectation)
|
14
11
|
end
|
15
12
|
end
|
@@ -1,40 +1,31 @@
|
|
1
|
-
|
2
|
-
attr_accessor :target
|
3
|
-
def initialize(target, method=nil)
|
4
|
-
self.target = target
|
5
|
-
events << MockEvent.new(:expects, [method]) unless method.nil?
|
6
|
-
end
|
1
|
+
module Expectations::MockRecorder
|
7
2
|
|
8
|
-
def
|
9
|
-
|
3
|
+
def receive!(method)
|
4
|
+
method_stack << [:expects, [method]]
|
10
5
|
self
|
11
6
|
end
|
12
7
|
|
13
|
-
def
|
14
|
-
@
|
8
|
+
def method_stack
|
9
|
+
@method_stack ||= []
|
15
10
|
end
|
16
11
|
|
17
|
-
def method_missing(
|
18
|
-
super if
|
19
|
-
|
12
|
+
def method_missing(sym, *args)
|
13
|
+
super if method_stack.empty?
|
14
|
+
method_stack << [sym, args]
|
20
15
|
self
|
21
16
|
end
|
22
17
|
|
23
|
-
def
|
24
|
-
|
25
|
-
|
26
|
-
end
|
27
|
-
target
|
18
|
+
def subject!
|
19
|
+
method_stack.inject(subject) { |result, element| result.send element.first, *element.last }
|
20
|
+
subject
|
28
21
|
end
|
29
22
|
|
30
23
|
def verify
|
31
|
-
|
24
|
+
subject.verify
|
32
25
|
end
|
33
26
|
|
34
|
-
|
35
|
-
|
36
|
-
def initialize(method, args)
|
37
|
-
self.method, self.args = method, args
|
38
|
-
end
|
27
|
+
def mocha_error_message(ex)
|
28
|
+
ex.message
|
39
29
|
end
|
30
|
+
|
40
31
|
end
|
data/lib/expectations/object.rb
CHANGED
@@ -1,21 +1,13 @@
|
|
1
1
|
class Object
|
2
|
-
def to_receive(method)
|
3
|
-
Expectations::MockRecorder.new(self, method)
|
4
|
-
end
|
5
|
-
|
6
|
-
def to_delegate(method)
|
7
|
-
Expectations::DelegateRecorder.new(self, method)
|
8
|
-
end
|
9
|
-
|
10
2
|
def to
|
11
|
-
Expectations::
|
3
|
+
Expectations::Recorder.new(self)
|
12
4
|
end
|
13
5
|
|
14
6
|
def not
|
15
7
|
Not.new(self)
|
16
8
|
end
|
17
9
|
|
18
|
-
def
|
10
|
+
def not!
|
19
11
|
!self
|
20
12
|
end
|
21
13
|
|
@@ -27,7 +19,7 @@ class Object
|
|
27
19
|
end
|
28
20
|
|
29
21
|
def method_missing(sym, *args, &blk)
|
30
|
-
@subject.send(sym,*args,&blk).
|
22
|
+
@subject.send(sym,*args,&blk).not!
|
31
23
|
end
|
32
24
|
end
|
33
25
|
|
@@ -1,14 +1,17 @@
|
|
1
|
-
module Expectations::
|
1
|
+
module Expectations::RecordedExpectation
|
2
2
|
def execute
|
3
3
|
begin
|
4
4
|
mocha_setup
|
5
|
-
instance_exec expected.subject
|
6
|
-
if expected.verify
|
5
|
+
instance_exec expected.subject!, &block if block
|
6
|
+
if expected.verify!
|
7
7
|
self.extend(Expectations::Results::Fulfilled)
|
8
8
|
else
|
9
9
|
self.extend(Expectations::Results::StateBasedFailure)
|
10
|
-
self.message = expected.
|
10
|
+
self.message = expected.failure_message
|
11
11
|
end
|
12
|
+
rescue Mocha::ExpectationError => ex
|
13
|
+
self.extend(Expectations::Results::BehaviorBasedFailure)
|
14
|
+
self.message = expected.mocha_error_message(ex)
|
12
15
|
rescue Exception => ex
|
13
16
|
self.extend(Expectations::Results::Error)
|
14
17
|
self.exception = ex
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class Expectations::Recorder
|
2
|
+
|
3
|
+
attr_reader :subject
|
4
|
+
def initialize(subject)
|
5
|
+
@subject = subject
|
6
|
+
end
|
7
|
+
|
8
|
+
def receive(meth)
|
9
|
+
extend Expectations::MockRecorder
|
10
|
+
receive!(meth)
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def have
|
15
|
+
extend Expectations::StateBasedRecorder
|
16
|
+
message_parts << "to have"
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def be
|
21
|
+
extend Expectations::StateBasedRecorder
|
22
|
+
message_parts << "to be"
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def delegate(method)
|
27
|
+
extend Expectations::DelegateRecorder
|
28
|
+
delegate!(method)
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def subject!
|
33
|
+
subject
|
34
|
+
end
|
35
|
+
|
36
|
+
def not!
|
37
|
+
extend Expectations::ReverseResult
|
38
|
+
end
|
39
|
+
|
40
|
+
def verify!
|
41
|
+
verify
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -1,29 +1,26 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
def
|
4
|
-
|
5
|
-
@message_parts = []
|
1
|
+
module Expectations::StateBasedRecorder
|
2
|
+
|
3
|
+
def verify
|
4
|
+
method_stack.inject(subject) { |result, element| result.send element.first, *element.last }
|
6
5
|
end
|
7
|
-
|
8
|
-
def
|
9
|
-
@message_parts
|
10
|
-
self
|
11
|
-
end
|
12
|
-
|
13
|
-
def be
|
14
|
-
@message_parts << "be"
|
15
|
-
self
|
6
|
+
|
7
|
+
def failure_message
|
8
|
+
"expected #{subject} #{@message_parts.join(" ")}"
|
16
9
|
end
|
17
|
-
|
10
|
+
|
18
11
|
def method_stack
|
19
12
|
@method_stack ||= []
|
20
13
|
end
|
21
14
|
|
15
|
+
def message_parts
|
16
|
+
@message_parts ||= self.is_a?(Expectations::ReverseResult) ? [:not] : []
|
17
|
+
end
|
18
|
+
|
22
19
|
def method_missing(sym, *args)
|
23
20
|
@message_parts << "#{sym}"
|
24
21
|
args.each { |arg| @message_parts << arg.inspect }
|
25
22
|
method_stack << [sym, args]
|
26
23
|
self
|
27
24
|
end
|
28
|
-
|
25
|
+
|
29
26
|
end
|
data/lib/expectations/suite.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
class Expectations::Suite
|
2
2
|
|
3
|
-
|
4
|
-
Expectations::MockRecorder.new(Mocha::Mock.new)
|
5
|
-
end
|
3
|
+
include Mocha::Standalone
|
6
4
|
|
7
5
|
def xml(string)
|
8
6
|
Expectations::XmlString.new(string)
|
@@ -20,7 +18,7 @@ class Expectations::Suite
|
|
20
18
|
end
|
21
19
|
|
22
20
|
def expect(expected, &block)
|
23
|
-
expectations << Expectations::Expectation.new(expected, &block)
|
21
|
+
expectations << Expectations::Expectation.new(expected, *caller.first.split(/:/), &block)
|
24
22
|
end
|
25
23
|
|
26
24
|
def do_not_run
|
@@ -32,7 +30,6 @@ class Expectations::Suite
|
|
32
30
|
[expectations.inject { |result, expectation| expectation.line > line.to_i ? result : expectation }]
|
33
31
|
end
|
34
32
|
|
35
|
-
private
|
36
33
|
def expectations
|
37
34
|
@expectations ||= []
|
38
35
|
end
|
data/lib/expectations.rb
CHANGED
@@ -16,12 +16,11 @@ require 'benchmark'
|
|
16
16
|
require 'erb'
|
17
17
|
require 'fileutils'
|
18
18
|
require File.expand_path(File.dirname(__FILE__) + '/expectations/object')
|
19
|
-
require File.expand_path(File.dirname(__FILE__) + '/expectations/
|
19
|
+
require File.expand_path(File.dirname(__FILE__) + '/expectations/recorder')
|
20
20
|
require File.expand_path(File.dirname(__FILE__) + '/expectations/delegate_recorder')
|
21
|
-
require File.expand_path(File.dirname(__FILE__) + '/expectations/
|
21
|
+
require File.expand_path(File.dirname(__FILE__) + '/expectations/recorded_expectation')
|
22
22
|
require File.expand_path(File.dirname(__FILE__) + '/expectations/state_based_recorder')
|
23
|
-
require File.expand_path(File.dirname(__FILE__) + '/expectations/
|
24
|
-
require File.expand_path(File.dirname(__FILE__) + '/expectations/negative_state_based_recorder')
|
23
|
+
require File.expand_path(File.dirname(__FILE__) + '/expectations/reverse_result')
|
25
24
|
require File.expand_path(File.dirname(__FILE__) + '/expectations/xml_string')
|
26
25
|
require File.expand_path(File.dirname(__FILE__) + '/expectations/regexp')
|
27
26
|
require File.expand_path(File.dirname(__FILE__) + '/expectations/range')
|
@@ -31,7 +30,6 @@ require File.expand_path(File.dirname(__FILE__) + '/expectations/suite')
|
|
31
30
|
require File.expand_path(File.dirname(__FILE__) + '/expectations/suite_runner')
|
32
31
|
require File.expand_path(File.dirname(__FILE__) + '/expectations/suite_results')
|
33
32
|
require File.expand_path(File.dirname(__FILE__) + '/expectations/expectation')
|
34
|
-
require File.expand_path(File.dirname(__FILE__) + '/expectations/behavior_based_expectation')
|
35
33
|
require File.expand_path(File.dirname(__FILE__) + '/expectations/state_based_expectation')
|
36
34
|
require File.expand_path(File.dirname(__FILE__) + '/expectations/mock_recorder')
|
37
35
|
require File.expand_path(File.dirname(__FILE__) + '/expectations/results')
|
data/rakefile.rb
CHANGED
@@ -46,7 +46,7 @@ specification = Gem::Specification.new do |s|
|
|
46
46
|
expect NoMethodError do
|
47
47
|
Object.invalid_method_call
|
48
48
|
end."
|
49
|
-
s.version = "0.2.
|
49
|
+
s.version = "0.2.3"
|
50
50
|
s.author = 'Jay Fields'
|
51
51
|
s.description = "A lightweight unit testing framework. Tests (expectations) will be written as follows
|
52
52
|
expect 2 do
|
@@ -2,43 +2,27 @@ require File.dirname(__FILE__) + "/../test_helper"
|
|
2
2
|
|
3
3
|
Expectations do
|
4
4
|
expect Expectations::Results::StateBasedFailure do
|
5
|
-
Expectations::Expectation.new(1) { 2 }.execute
|
5
|
+
Expectations::Expectation.new(1, nil, nil) { 2 }.execute
|
6
6
|
end
|
7
7
|
|
8
8
|
expect Expectations::Results::Fulfilled do
|
9
|
-
Expectations::Expectation.new(1) { 1 }.execute
|
9
|
+
Expectations::Expectation.new(1, nil, nil) { 1 }.execute
|
10
10
|
end
|
11
11
|
|
12
12
|
expect Expectations::Results::Error do
|
13
|
-
Expectations::Expectation.new(1) { raise }.execute
|
13
|
+
Expectations::Expectation.new(1, nil, nil) { raise }.execute
|
14
14
|
end
|
15
15
|
|
16
16
|
expect "undefined method `no_method' for Object:Class" do
|
17
|
-
Expectations::Expectation.new(ArgumentError) { Object.no_method }.execute.exception.to_s
|
17
|
+
Expectations::Expectation.new(ArgumentError, nil, nil) { Object.no_method }.execute.exception.to_s
|
18
18
|
end
|
19
19
|
|
20
20
|
expect Expectations::Results::Fulfilled do
|
21
|
-
Expectations::Expectation.new(NoMethodError) { Object.no_method }.execute
|
21
|
+
Expectations::Expectation.new(NoMethodError, nil, nil) { Object.no_method }.execute
|
22
22
|
end
|
23
23
|
|
24
24
|
expect nil do
|
25
|
-
Expectations::Expectation.new(String) { Object.no_method }.execute.actual
|
25
|
+
Expectations::Expectation.new(String, nil, nil) { Object.no_method }.execute.actual
|
26
26
|
end
|
27
27
|
|
28
|
-
expect __LINE__ + 1 do
|
29
|
-
Expectations::Expectation.new(1) { 2 }.execute.line
|
30
|
-
end
|
31
|
-
|
32
|
-
expect __LINE__ + 1 do
|
33
|
-
Expectations::Expectation.new(1) { raise }.execute.line
|
34
|
-
end
|
35
|
-
|
36
|
-
expect __FILE__ do
|
37
|
-
Expectations::Expectation.new(1) { 2 }.execute.file
|
38
|
-
end
|
39
|
-
|
40
|
-
expect __FILE__ do
|
41
|
-
Expectations::Expectation.new(1) { raise }.execute.file
|
42
|
-
end
|
43
|
-
|
44
28
|
end
|
@@ -2,11 +2,11 @@ require File.dirname(__FILE__) + "/../test_helper"
|
|
2
2
|
|
3
3
|
Expectations do
|
4
4
|
|
5
|
-
expect Expectations::SuiteRunner.
|
5
|
+
expect Expectations::SuiteRunner.to.receive(:suite_eval) do |suite|
|
6
6
|
Expectations { }
|
7
7
|
end
|
8
8
|
|
9
|
-
expect Expectations::SuiteRunner.
|
9
|
+
expect Expectations::SuiteRunner.to.receive(:do_not_run) do |suite|
|
10
10
|
Expectations { Object.no_method_error_should_raise } rescue nil
|
11
11
|
end
|
12
12
|
|
@@ -1,12 +1,12 @@
|
|
1
1
|
require File.dirname(__FILE__) + "/../test_helper"
|
2
2
|
|
3
3
|
Expectations do
|
4
|
-
expect mock.
|
4
|
+
expect mock.to.receive(:instance_eval) do |suite|
|
5
5
|
Expectations::SuiteRunner.stubs(:instance).returns stub(:suite => suite)
|
6
6
|
Expectations::SuiteRunner.suite_eval {}
|
7
7
|
end
|
8
8
|
|
9
|
-
expect mock.
|
9
|
+
expect mock.to.receive(:do_not_run) do |suite|
|
10
10
|
Expectations::SuiteRunner.stubs(:instance).returns stub(:suite => suite)
|
11
11
|
Expectations::SuiteRunner.do_not_run
|
12
12
|
end
|
@@ -20,12 +20,8 @@ Expectations do
|
|
20
20
|
suite.execute(Silent).succeeded?
|
21
21
|
end
|
22
22
|
|
23
|
-
expect Expectations::MockRecorder do
|
24
|
-
Expectations::Suite.new.mock
|
25
|
-
end
|
26
|
-
|
27
23
|
expect Mocha::Mock do
|
28
|
-
Expectations::Suite.new.mock
|
24
|
+
Expectations::Suite.new.mock
|
29
25
|
end
|
30
26
|
|
31
27
|
expect 3 do
|
@@ -50,4 +46,28 @@ Expectations do
|
|
50
46
|
suite.expectations_for(__LINE__ - 1).first.expected
|
51
47
|
end
|
52
48
|
|
49
|
+
expect __LINE__ + 2 do
|
50
|
+
suite = Expectations::Suite.new
|
51
|
+
suite.expect(1) { 2 }
|
52
|
+
suite.expectations.first.line
|
53
|
+
end
|
54
|
+
|
55
|
+
expect __LINE__ + 2 do
|
56
|
+
suite = Expectations::Suite.new
|
57
|
+
suite.expect(1) { raise }
|
58
|
+
suite.expectations.first.line
|
59
|
+
end
|
60
|
+
|
61
|
+
expect __FILE__ do
|
62
|
+
suite = Expectations::Suite.new
|
63
|
+
suite.expect(1) { 2 }
|
64
|
+
suite.expectations.first.file
|
65
|
+
end
|
66
|
+
|
67
|
+
expect __FILE__ do
|
68
|
+
suite = Expectations::Suite.new
|
69
|
+
suite.expect(1) { raise }
|
70
|
+
suite.expectations.first.file
|
71
|
+
end
|
72
|
+
|
53
73
|
end
|
data/test/failures_test.rb
CHANGED
@@ -15,7 +15,7 @@ Expectations do
|
|
15
15
|
|
16
16
|
expect Expectations::Results::BehaviorBasedFailure do
|
17
17
|
suite = Expectations::Suite.new
|
18
|
-
suite.expect
|
18
|
+
suite.expect Mocha::Mock.new.to.receive(:dial).with("2125551212").times(2) do |phone|
|
19
19
|
phone.dial("2125551212")
|
20
20
|
phone.dial("2125551212")
|
21
21
|
phone.dial("2125551212")
|
@@ -25,13 +25,15 @@ Expectations do
|
|
25
25
|
|
26
26
|
expect Expectations::Results::BehaviorBasedFailure do
|
27
27
|
suite = Expectations::Suite.new
|
28
|
-
suite.expect
|
28
|
+
suite.expect Mocha::Mock.new.to.receive(:dial).with("2125551212").times(2) do |phone|
|
29
|
+
phone.dial("2125551212")
|
30
|
+
end
|
29
31
|
suite.execute(Silent).expectations.first
|
30
32
|
end
|
31
33
|
|
32
34
|
expect Expectations::Results::BehaviorBasedFailure do
|
33
35
|
suite = Expectations::Suite.new
|
34
|
-
suite.expect(Object.
|
36
|
+
suite.expect(Object.to.receive(:deal)) { }
|
35
37
|
suite.execute(Silent).expectations.first
|
36
38
|
end
|
37
39
|
|
data/test/successes_test.rb
CHANGED
@@ -13,13 +13,25 @@ Expectations do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
# Behavior based test using a traditional mock
|
16
|
-
expect mock.
|
16
|
+
expect mock.to.receive(:dial).with("2125551212").times(2) do |phone|
|
17
|
+
phone.dial("2125551212")
|
18
|
+
phone.dial("2125551212")
|
19
|
+
end
|
20
|
+
|
21
|
+
# Behavior based test using a stub
|
22
|
+
expect stub.to.receive(:dial).with("2125551212").times(2) do |phone|
|
23
|
+
phone.dial("2125551212")
|
24
|
+
phone.dial("2125551212")
|
25
|
+
end
|
26
|
+
|
27
|
+
# Behavior based test using a stub_everything
|
28
|
+
expect stub_everything.to.receive(:dial).with("2125551212").times(2) do |phone|
|
17
29
|
phone.dial("2125551212")
|
18
30
|
phone.dial("2125551212")
|
19
31
|
end
|
20
32
|
|
21
33
|
# Behavior based test on a concrete mock
|
22
|
-
expect Object.
|
34
|
+
expect Object.to.receive(:deal) do
|
23
35
|
Object.deal
|
24
36
|
end
|
25
37
|
|
@@ -96,8 +108,26 @@ Expectations do
|
|
96
108
|
end
|
97
109
|
end
|
98
110
|
# State based delegation test
|
99
|
-
expect klass.new.
|
111
|
+
expect klass.new.to.delegate(:save).to(:record) do |instance|
|
100
112
|
instance.save(1)
|
101
113
|
end
|
102
114
|
|
115
|
+
# this is normally defined in the file specific to the class
|
116
|
+
klass = Class.new do
|
117
|
+
attr_accessor :started
|
118
|
+
end
|
119
|
+
# State based fluent interface boolean test using to be
|
120
|
+
expect klass.new.to.be.started do |process|
|
121
|
+
process.started = true
|
122
|
+
end
|
123
|
+
|
124
|
+
# this is normally defined in the file specific to the class
|
125
|
+
klass = Class.new do
|
126
|
+
attr_accessor :finished
|
127
|
+
end
|
128
|
+
# State based fluent interface boolean test using to have
|
129
|
+
expect klass.new.to.have.finished do |process|
|
130
|
+
process.finished = true
|
131
|
+
end
|
132
|
+
|
103
133
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: expectations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jay Fields
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-03-
|
12
|
+
date: 2008-03-07 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -30,19 +30,17 @@ extensions: []
|
|
30
30
|
extra_rdoc_files:
|
31
31
|
- README
|
32
32
|
files:
|
33
|
-
- lib/expectations/behavior_based_expectation.rb
|
34
|
-
- lib/expectations/delegate_expectation.rb
|
35
33
|
- lib/expectations/delegate_recorder.rb
|
36
34
|
- lib/expectations/expectation.rb
|
37
35
|
- lib/expectations/mock_recorder.rb
|
38
36
|
- lib/expectations/module.rb
|
39
|
-
- lib/expectations/negative_state_based_recorder.rb
|
40
37
|
- lib/expectations/object.rb
|
41
|
-
- lib/expectations/positive_state_based_recorder.rb
|
42
38
|
- lib/expectations/range.rb
|
43
|
-
- lib/expectations/
|
39
|
+
- lib/expectations/recorded_expectation.rb
|
40
|
+
- lib/expectations/recorder.rb
|
44
41
|
- lib/expectations/regexp.rb
|
45
42
|
- lib/expectations/results.rb
|
43
|
+
- lib/expectations/reverse_result.rb
|
46
44
|
- lib/expectations/state_based_expectation.rb
|
47
45
|
- lib/expectations/state_based_recorder.rb
|
48
46
|
- lib/expectations/string.rb
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module Expectations::BehaviorBasedExpectation
|
2
|
-
def execute
|
3
|
-
begin
|
4
|
-
mocha_setup
|
5
|
-
instance_exec expected.mock, &block
|
6
|
-
expected.verify
|
7
|
-
self.extend(Expectations::Results::Fulfilled)
|
8
|
-
rescue Mocha::ExpectationError => ex
|
9
|
-
self.extend(Expectations::Results::BehaviorBasedFailure)
|
10
|
-
self.message = ex.message
|
11
|
-
rescue Exception => ex
|
12
|
-
self.extend(Expectations::Results::Error)
|
13
|
-
self.exception = ex
|
14
|
-
ensure
|
15
|
-
mocha_teardown
|
16
|
-
end
|
17
|
-
self
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module Expectations::DelegateExpectation
|
2
|
-
def execute
|
3
|
-
begin
|
4
|
-
mocha_setup
|
5
|
-
actual = instance_exec expected.mock, &block
|
6
|
-
if expected.verify(actual)
|
7
|
-
self.extend(Expectations::Results::Fulfilled)
|
8
|
-
else
|
9
|
-
self.extend(Expectations::Results::StateBasedFailure)
|
10
|
-
self.message = "Delegation may have occurred; however, the return value could not be verified. Did you specify something after the delegation method?"
|
11
|
-
end
|
12
|
-
rescue Mocha::ExpectationError => ex
|
13
|
-
self.extend(Expectations::Results::BehaviorBasedFailure)
|
14
|
-
self.message = "Expected #{expected.klass} to delegate #{expected.meth} to #{expected.subject}; however, #{expected.subject}.#{expected.meth} was never called"
|
15
|
-
rescue Exception => ex
|
16
|
-
self.extend(Expectations::Results::Error)
|
17
|
-
self.exception = ex
|
18
|
-
ensure
|
19
|
-
mocha_teardown
|
20
|
-
end
|
21
|
-
self
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
module Expectations::PositiveStateBasedRecorder
|
2
|
-
|
3
|
-
def verify
|
4
|
-
method_stack.inject(subject) { |result, element| result.send element.first, *element.last }
|
5
|
-
end
|
6
|
-
|
7
|
-
def __negate__
|
8
|
-
extend Expectations::NegativeStateBasedRecorder
|
9
|
-
end
|
10
|
-
|
11
|
-
def message
|
12
|
-
"expected #{subject} to #{@message_parts.join(" ")}"
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|