motion-stump 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +13 -4
- data/lib/motion-stump.rb +3 -2
- data/{app → lib/stump}/app_delegate.rb +0 -0
- data/lib/stump/mock.rb +12 -0
- data/lib/stump/stub.rb +13 -7
- data/lib/stump/version.rb +1 -1
- data/spec/main_spec.rb +129 -30
- metadata +10 -22
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 582335357cf4b0b510e0ebb140abfd520f9c05bb
|
4
|
+
data.tar.gz: e1571647e3da93c289ef8d443565ab13b00c9722
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 37cd5afe544468ce18afe677f6eb0020d6111845f605bf1ebc26916877e5b30431978045a189b1d0d269de58d8c0850f558ed5ab9dc8dc69a5c09b00d6e835b5
|
7
|
+
data.tar.gz: d32793edd2866cf12ea63245cf4ef278b0c372fbc1c195ce669a251b267aa7bb9d137db4f9b525f4c478238bb855d5876fb57b6bd31992ca47ab404631f4e81c
|
data/README.md
CHANGED
@@ -7,10 +7,19 @@ Stubbing and mocking for RubyMotion. Based on Jeremy McAnally's [stump](https://
|
|
7
7
|
### Stubbing right on the object
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
#
|
11
|
-
|
12
|
-
#
|
13
|
-
|
10
|
+
MyClass.stub!(:thing, :return => "hey!") # => 'hey!'
|
11
|
+
|
12
|
+
your_object.stub!(:hello) # => nil
|
13
|
+
|
14
|
+
your_object.stub!(:thing) do |arg1, arg2|
|
15
|
+
"Hello #{arg1},#{arg2}"
|
16
|
+
end
|
17
|
+
your_object.thing("1", "2") # => 'Hello 1,2'
|
18
|
+
|
19
|
+
your_object.stub!(:hello, :yield => ["1", "2"])
|
20
|
+
your_object.hello do |a, b| # yield with "1" and "2"
|
21
|
+
puts "#{a}, #{b}"
|
22
|
+
end
|
14
23
|
```
|
15
24
|
|
16
25
|
### Pure stub objects
|
data/lib/motion-stump.rb
CHANGED
@@ -23,7 +23,8 @@ Motion::Project::App.setup do |app|
|
|
23
23
|
File.join(File.dirname(__FILE__), 'stump/stub.rb'),
|
24
24
|
File.join(File.dirname(__FILE__), 'stump/mocks.rb'),
|
25
25
|
File.join(File.dirname(__FILE__), 'stump/mock.rb'),
|
26
|
-
File.join(File.dirname(__FILE__), 'stump/proxy.rb')
|
26
|
+
File.join(File.dirname(__FILE__), 'stump/proxy.rb'),
|
27
|
+
File.join(File.dirname(__FILE__), 'stump/app_delegate.rb')
|
27
28
|
].reverse.each {|f| app.files.unshift(f) }
|
28
29
|
end
|
29
|
-
end
|
30
|
+
end
|
File without changes
|
data/lib/stump/mock.rb
CHANGED
@@ -23,6 +23,11 @@ class Object
|
|
23
23
|
Stump::Mocks.verify([self, method])
|
24
24
|
block.call(*args)
|
25
25
|
end
|
26
|
+
elsif options[:yield]
|
27
|
+
lambda do |*args|
|
28
|
+
Stump::Mocks.verify([self, method])
|
29
|
+
yield(options[:yield])
|
30
|
+
end
|
26
31
|
else
|
27
32
|
lambda do |*args|
|
28
33
|
Stump::Mocks.verify([self, method])
|
@@ -32,6 +37,13 @@ class Object
|
|
32
37
|
|
33
38
|
meta_def method, &behavior
|
34
39
|
end
|
40
|
+
|
41
|
+
def should_not_call(method)
|
42
|
+
behavior = lambda do |*args|
|
43
|
+
should.flunk "Umet expectations: #{method} expected to not be called"
|
44
|
+
end
|
45
|
+
meta_def method, &behavior
|
46
|
+
end
|
35
47
|
end
|
36
48
|
|
37
49
|
module Kernel
|
data/lib/stump/stub.rb
CHANGED
@@ -1,19 +1,25 @@
|
|
1
1
|
class Object
|
2
2
|
# Create a stub method on an object. Simply returns a value for a method call on
|
3
3
|
# an object.
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# ==== Examples
|
6
6
|
#
|
7
7
|
# my_string = "a wooden rabbit"
|
8
8
|
# my_string.stub!(:retreat!, :return => "run away! run away!")
|
9
|
-
#
|
9
|
+
#
|
10
10
|
# # test/your_test.rb
|
11
11
|
# my_string.retreat! # => "run away! run away!"
|
12
12
|
#
|
13
|
-
def stub!(
|
14
|
-
behavior = (block_given? ?
|
13
|
+
def stub!(method_name, options = {}, &stubbed)
|
14
|
+
behavior = (block_given? ? stubbed : lambda { return options[:return] })
|
15
15
|
|
16
|
-
|
16
|
+
class << self
|
17
|
+
self
|
18
|
+
end.instance_eval {
|
19
|
+
define_method(method_name) { |*args, &block|
|
20
|
+
behavior.call(*args, &block)
|
21
|
+
}
|
22
|
+
}
|
17
23
|
end
|
18
24
|
end
|
19
25
|
|
@@ -21,14 +27,14 @@ module Kernel
|
|
21
27
|
# Create a pure stub object.
|
22
28
|
#
|
23
29
|
# ==== Examples
|
24
|
-
#
|
30
|
+
#
|
25
31
|
# stubbalicious = stub(:failure, "wat u say?")
|
26
32
|
# stubbalicious.failure # => "wat u say?"
|
27
33
|
#
|
28
34
|
def stub(method, options = {}, &block)
|
29
35
|
stub_object = Object.new
|
30
36
|
stub_object.stub!(method, options, &block)
|
31
|
-
|
37
|
+
|
32
38
|
stub_object
|
33
39
|
end
|
34
40
|
end
|
data/lib/stump/version.rb
CHANGED
data/spec/main_spec.rb
CHANGED
@@ -1,45 +1,144 @@
|
|
1
1
|
describe "Application 'stump-test'" do
|
2
|
-
|
2
|
+
|
3
|
+
# Test class for mocking.
|
4
|
+
class Dog
|
5
|
+
def self.create
|
6
|
+
return Dog.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def bark
|
10
|
+
"Woof!"
|
11
|
+
end
|
12
|
+
|
13
|
+
def eat(food)
|
14
|
+
"#{food}! Yum!"
|
15
|
+
end
|
16
|
+
|
17
|
+
# Calls the given block on the nearest toy, a stuffed mouse.
|
18
|
+
def get_toy(&block)
|
19
|
+
block.call("stuffed mouse")
|
20
|
+
end
|
21
|
+
|
22
|
+
# Calls the given block on true (for a succesful fetch) and 2 (the time it
|
23
|
+
# took to fetch.
|
24
|
+
def fetch(&block)
|
25
|
+
block.call(true, 20)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
before do
|
30
|
+
@dog = Dog.new
|
3
31
|
end
|
4
32
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
33
|
+
describe "#stub!" do
|
34
|
+
it "should stub a class method" do
|
35
|
+
Dog.stub!(:thing, return: :thing)
|
36
|
+
Dog.should.not.be.nil
|
37
|
+
Dog.thing.should == :thing
|
38
|
+
end
|
9
39
|
|
10
|
-
|
11
|
-
|
12
|
-
|
40
|
+
it "should stub an instance method" do
|
41
|
+
my_obj = Object.new
|
42
|
+
my_obj.stub!(:hello, return: "hi")
|
43
|
+
my_obj.hello.should.be == "hi"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should stub using block" do
|
47
|
+
my_obj = Object.new
|
48
|
+
my_obj.stub!(:hello) do |a, b|
|
49
|
+
a.should == "foo"
|
50
|
+
b.should == "bar"
|
51
|
+
"#{a},#{b}"
|
52
|
+
end
|
53
|
+
my_obj.hello("foo", "bar").should.be == "foo,bar"
|
54
|
+
end
|
13
55
|
end
|
56
|
+
|
57
|
+
describe "#stub" do
|
58
|
+
it "should create a pure stub" do
|
59
|
+
my_stub = stub(:thing, return: "dude, a thing!")
|
60
|
+
my_stub.thing.should == "dude, a thing!"
|
61
|
+
end
|
14
62
|
|
15
|
-
|
16
|
-
|
17
|
-
|
63
|
+
it "should create a stub using block" do
|
64
|
+
my_stub = stub(:thing) do |a, b|
|
65
|
+
a.should == "a"
|
66
|
+
b.should == "thing!"
|
67
|
+
"dude, #{a} #{b}"
|
68
|
+
end
|
69
|
+
my_stub.thing("a", "thing!").should == "dude, a thing!"
|
70
|
+
end
|
18
71
|
end
|
19
72
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
73
|
+
describe "#mock!" do
|
74
|
+
it "should mock an instance method on an object" do
|
75
|
+
@dog.mock!(:bark, return: "Meow!")
|
76
|
+
@dog.mock!(:eat, return: "Yuck!")
|
77
|
+
@dog.bark.should == "Meow!"
|
78
|
+
@dog.eat("broccoli").should == "Yuck!"
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should mock using block" do
|
82
|
+
@dog.mock!(:bark) do |a|
|
83
|
+
a.should == "Meow!"
|
84
|
+
a
|
85
|
+
end
|
86
|
+
@dog.bark("Meow!").should == "Meow!"
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should mock a class method" do
|
90
|
+
Dog.mock!(:create, return: "Dog")
|
91
|
+
Dog.create.should == "Dog"
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should be able to yield a single object" do
|
95
|
+
@dog.mock!(:get_toy, yield: "stuffed fox")
|
96
|
+
@dog.get_toy do |toy|
|
97
|
+
toy.should.be == "stuffed fox"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should be able to yield multiple objects" do
|
102
|
+
@dog.mock!(:fetch, yield: [false, 10])
|
103
|
+
@dog.fetch do |success, time|
|
104
|
+
success.should.be == success
|
105
|
+
time.should.be == 10
|
106
|
+
end
|
107
|
+
end
|
26
108
|
end
|
27
109
|
|
28
|
-
|
29
|
-
|
30
|
-
|
110
|
+
describe "#mock" do
|
111
|
+
it "should create pure mock" do
|
112
|
+
my_mock = mock(:hello, return: "hi")
|
113
|
+
my_mock.hello.should == "hi"
|
114
|
+
end
|
31
115
|
end
|
32
116
|
|
33
|
-
#
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
117
|
+
describe "#should_not_call" do
|
118
|
+
it "should raise an error with an instance method" do
|
119
|
+
@dog.should_not_call(:bark)
|
120
|
+
should.raise(Bacon::Error) do
|
121
|
+
@dog.bark
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should succeed if the call is not made" do
|
126
|
+
should.not.raise(Bacon::Error) do
|
127
|
+
@dog.should_not_call(:bark)
|
128
|
+
end
|
129
|
+
end
|
38
130
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
131
|
+
it "should not fail in another test" do
|
132
|
+
should.not.raise(Bacon::Error) do
|
133
|
+
@dog.bark
|
134
|
+
end
|
135
|
+
end
|
44
136
|
|
137
|
+
it "should raise an error with a class method" do
|
138
|
+
Dog.should_not_call(:create)
|
139
|
+
should.raise(Bacon::Error) do
|
140
|
+
Dog.create
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
45
144
|
end
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: motion-stump
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Francis Chong
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-03-17 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: motion-redgreen
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,17 +27,15 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - '>='
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - '>='
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
description: Stubbing and mocking for RubyMotion
|
@@ -56,8 +51,8 @@ files:
|
|
56
51
|
- License.txt
|
57
52
|
- README.md
|
58
53
|
- Rakefile
|
59
|
-
- app/app_delegate.rb
|
60
54
|
- lib/motion-stump.rb
|
55
|
+
- lib/stump/app_delegate.rb
|
61
56
|
- lib/stump/metaid.rb
|
62
57
|
- lib/stump/mock.rb
|
63
58
|
- lib/stump/mocks.rb
|
@@ -69,33 +64,26 @@ files:
|
|
69
64
|
- spec/main_spec.rb
|
70
65
|
homepage: https://github.com/siuying/motion-stump
|
71
66
|
licenses: []
|
67
|
+
metadata: {}
|
72
68
|
post_install_message:
|
73
69
|
rdoc_options: []
|
74
70
|
require_paths:
|
75
71
|
- lib
|
76
72
|
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
-
none: false
|
78
73
|
requirements:
|
79
|
-
- -
|
74
|
+
- - '>='
|
80
75
|
- !ruby/object:Gem::Version
|
81
76
|
version: '0'
|
82
|
-
segments:
|
83
|
-
- 0
|
84
|
-
hash: 800842307904232879
|
85
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
-
none: false
|
87
78
|
requirements:
|
88
|
-
- -
|
79
|
+
- - '>='
|
89
80
|
- !ruby/object:Gem::Version
|
90
81
|
version: '0'
|
91
|
-
segments:
|
92
|
-
- 0
|
93
|
-
hash: 800842307904232879
|
94
82
|
requirements: []
|
95
83
|
rubyforge_project:
|
96
|
-
rubygems_version:
|
84
|
+
rubygems_version: 2.0.0
|
97
85
|
signing_key:
|
98
|
-
specification_version:
|
86
|
+
specification_version: 4
|
99
87
|
summary: Stubbing and mocking for RubyMotion
|
100
88
|
test_files:
|
101
89
|
- spec/main_spec.rb
|