dupe 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dupe/mock.rb +10 -2
- data/spec/lib_specs/mock_spec.rb +49 -1
- metadata +2 -2
data/lib/dupe/mock.rb
CHANGED
@@ -3,6 +3,8 @@ class Dupe
|
|
3
3
|
class Mock #:nodoc:
|
4
4
|
include Dupe::Network::RestValidation
|
5
5
|
|
6
|
+
class ResourceNotFoundError < StandardError; end
|
7
|
+
|
6
8
|
attr_reader :verb
|
7
9
|
attr_reader :url_pattern
|
8
10
|
attr_reader :response
|
@@ -27,17 +29,23 @@ class Dupe
|
|
27
29
|
def mocked_response(url)
|
28
30
|
raise(
|
29
31
|
StandardError,
|
30
|
-
"Tried to mock a response to a non-matched url! This should never occur."
|
32
|
+
"Tried to mock a response to a non-matched url! This should never occur. My pattern: #{@url_pattern}. Url: #{url}"
|
31
33
|
) unless match?(url)
|
32
34
|
|
33
35
|
grouped_results = url_pattern.match(url)[1..-1]
|
34
36
|
resp = @response.call *grouped_results
|
35
37
|
|
36
38
|
case resp
|
39
|
+
when NilClass
|
40
|
+
raise ResourceNotFoundError, "Failed with 404: the request '#{url}' returned nil."
|
37
41
|
when Dupe::Database::Record
|
38
42
|
resp = resp.to_xml(:root => resp.__model__.name.to_s)
|
39
43
|
when Array
|
40
|
-
|
44
|
+
if resp.empty?
|
45
|
+
resp = [].to_xml :root => 'results'
|
46
|
+
else
|
47
|
+
resp = resp.to_xml(:root => resp.first.__model__.name.to_s.pluralize)
|
48
|
+
end
|
41
49
|
end
|
42
50
|
|
43
51
|
Dupe.network.log.add_request @verb, url, resp
|
data/spec/lib_specs/mock_spec.rb
CHANGED
@@ -42,13 +42,19 @@ describe Dupe::Network::Mock do
|
|
42
42
|
end
|
43
43
|
|
44
44
|
describe "mocked_response" do
|
45
|
-
describe "on a mock object whose response returns a Dupe.find" do
|
45
|
+
describe "on a mock object whose response returns a Dupe.find with actual results" do
|
46
46
|
it "should convert the response result to xml" do
|
47
47
|
url_pattern = %r{/books/(\d+)\.xml}
|
48
48
|
response = proc {|id| Dupe.find(:book) {|b| b.id == id.to_i}}
|
49
49
|
book = Dupe.create :book
|
50
50
|
mock = Dupe::Network::Mock.new :get, url_pattern, response
|
51
51
|
mock.mocked_response('/books/1.xml').should == book.to_xml(:root => 'book')
|
52
|
+
|
53
|
+
proc { mock.mocked_response('/books/2.xml') }.should raise_error(Dupe::Network::Mock::ResourceNotFoundError)
|
54
|
+
|
55
|
+
Dupe.define :author
|
56
|
+
mock = Dupe::Network::Mock.new :get, %r{/authors\.xml$}, proc {Dupe.find :authors}
|
57
|
+
mock.mocked_response('/authors.xml').should == [].to_xml(:root => 'results')
|
52
58
|
end
|
53
59
|
|
54
60
|
it "should add a request to the Dupe::Network#log" do
|
@@ -61,6 +67,48 @@ describe Dupe::Network::Mock do
|
|
61
67
|
Dupe.network.log.requests.length.should == 1
|
62
68
|
end
|
63
69
|
end
|
70
|
+
|
71
|
+
describe "on a mock object whose response returns nil" do
|
72
|
+
it "should raise an error" do
|
73
|
+
url_pattern = %r{/authors/(\d+)\.xml}
|
74
|
+
response = proc { |id| Dupe.find(:author) {|a| a.id == id.to_i}}
|
75
|
+
Dupe.define :author
|
76
|
+
mock = Dupe::Network::Mock.new :get, url_pattern, response
|
77
|
+
proc {mock.mocked_response('/authors/1.xml')}.should raise_error(Dupe::Network::Mock::ResourceNotFoundError)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "on a mock object whose response returns an empty array" do
|
82
|
+
it "should convert the empty array to an xml array record set with root 'results'" do
|
83
|
+
Dupe.define :author
|
84
|
+
mock = Dupe::Network::Mock.new :get, %r{/authors\.xml$}, proc {Dupe.find :authors}
|
85
|
+
mock.mocked_response('/authors.xml').should == [].to_xml(:root => 'results')
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should add a request to the Dupe::Network#log" do
|
89
|
+
Dupe.define :author
|
90
|
+
mock = Dupe::Network::Mock.new :get, %r{/authors\.xml$}, proc {Dupe.find :authors}
|
91
|
+
Dupe.network.log.requests.length.should == 0
|
92
|
+
mock.mocked_response('/authors.xml')
|
93
|
+
Dupe.network.log.requests.length.should == 1
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "on a mock object whose response returns an array of duped records" do
|
98
|
+
it "should convert the array to xml" do
|
99
|
+
Dupe.create :author
|
100
|
+
mock = Dupe::Network::Mock.new :get, %r{/authors\.xml$}, proc {Dupe.find :authors}
|
101
|
+
mock.mocked_response('/authors.xml').should == Dupe.find(:authors).to_xml(:root => 'authors')
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should add a request to the Dupe::Network#log" do
|
105
|
+
Dupe.create :author
|
106
|
+
mock = Dupe::Network::Mock.new :get, %r{/authors\.xml$}, proc {Dupe.find :authors}
|
107
|
+
Dupe.network.log.requests.length.should == 0
|
108
|
+
mock.mocked_response('/authors.xml')
|
109
|
+
Dupe.network.log.requests.length.should == 1
|
110
|
+
end
|
111
|
+
end
|
64
112
|
end
|
65
113
|
|
66
114
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dupe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Parker
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-01-01 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|