response_code_matchers 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +16 -0
- data/lib/response_code_matchers.rb +30 -7
- data/lib/response_code_matchers/version.rb +1 -1
- data/spec/response_code_matchers_spec.rb +10 -0
- metadata +4 -4
data/README.md
CHANGED
@@ -59,6 +59,22 @@ end
|
|
59
59
|
```
|
60
60
|
|
61
61
|
|
62
|
+
## Advantage
|
63
|
+
[Rack::Response](https://github.com/rack/rack/blob/master/lib/rack/response.rb) has predicative methods like `#not_found?`, `#bad_request?`, and etc. so we can use `response.should be_not_found` without this gem.
|
64
|
+
|
65
|
+
There are 2 advantages to use this gem.
|
66
|
+
1. The range of differences that Rack::Response does not have
|
67
|
+
2. Useful failure messages for each failed reason
|
68
|
+
|
69
|
+
```
|
70
|
+
# without response_code_matchers.gem
|
71
|
+
expected not_found? to return true, got false
|
72
|
+
|
73
|
+
# with response_code_matchers.gem
|
74
|
+
expected response code to be 404, but 400
|
75
|
+
```
|
76
|
+
|
77
|
+
|
62
78
|
## Matchers
|
63
79
|
```ruby
|
64
80
|
100: response.should be_continue
|
@@ -11,25 +11,48 @@ module ResponseCodeMatchers
|
|
11
11
|
|
12
12
|
class ResponseCodeMatcher
|
13
13
|
def initialize(expected, name)
|
14
|
-
@expected
|
15
|
-
@
|
14
|
+
@expected = expected
|
15
|
+
@name = name
|
16
16
|
end
|
17
17
|
|
18
18
|
def matches?(response)
|
19
|
-
@
|
20
|
-
|
19
|
+
@valid = response.respond_to?(:code)
|
20
|
+
if @valid
|
21
|
+
@actual = response.code
|
22
|
+
@actual == @expected
|
23
|
+
else
|
24
|
+
response.__send__(method_name)
|
25
|
+
end
|
21
26
|
end
|
22
27
|
|
23
28
|
def description
|
24
|
-
"be
|
29
|
+
"be #{human_name}"
|
25
30
|
end
|
26
31
|
|
27
32
|
def failure_message
|
28
|
-
|
33
|
+
if @valid
|
34
|
+
"expected response code to be #@expected, but #@actual"
|
35
|
+
else
|
36
|
+
"expected #{method_name} to return true, got false"
|
37
|
+
end
|
29
38
|
end
|
30
39
|
|
31
40
|
def negative_failure_message
|
32
|
-
|
41
|
+
if @valid
|
42
|
+
"expected response code not to be #@expected, but #@actual"
|
43
|
+
else
|
44
|
+
"expected #{method_name} to return false, got true"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def method_name
|
51
|
+
"#@name?"
|
52
|
+
end
|
53
|
+
|
54
|
+
def human_name
|
55
|
+
@name.gsub("_", " ")
|
33
56
|
end
|
34
57
|
end
|
35
58
|
end
|
@@ -65,4 +65,14 @@ describe ResponseCodeMatchers do
|
|
65
65
|
end
|
66
66
|
end
|
67
67
|
end
|
68
|
+
|
69
|
+
context "when receiver does not have a method #code" do
|
70
|
+
let(:receiver) do
|
71
|
+
mock(:accepted? => true)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "calls original receiver.xxx?" do
|
75
|
+
receiver.should be_accepted
|
76
|
+
end
|
77
|
+
end
|
68
78
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: response_code_matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ryo NAKAMURA
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-12-05 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rack
|