Commit 9a5599367eeeaa073f357859a53e232b6868f136
1 parent
9d25396c
Exists in
master
WxPay::Result should return nil for nonexistent key
Showing
3 changed files
with
29 additions
and
8 deletions
Show diff stats
lib/wx_pay/result.rb
... | ... | @@ -2,14 +2,16 @@ module WxPay |
2 | 2 | class Result < ::Hash |
3 | 3 | SUCCESS_FLAG = 'SUCCESS'.freeze |
4 | 4 | |
5 | - def initialize(result) | |
6 | - super | |
5 | + def self.[] result | |
6 | + hash = super | |
7 | 7 | |
8 | 8 | if result['xml'].class == Hash |
9 | 9 | result['xml'].each_pair do |k, v| |
10 | - self[k] = v | |
10 | + hash[k] = v | |
11 | 11 | end |
12 | 12 | end |
13 | + | |
14 | + hash | |
13 | 15 | end |
14 | 16 | |
15 | 17 | def success? | ... | ... |
lib/wx_pay/service.rb
test/wx_pay/result_test.rb
... | ... | @@ -2,7 +2,7 @@ require 'test_helper' |
2 | 2 | |
3 | 3 | class WxPay::ResultTest < MiniTest::Test |
4 | 4 | def test_success_method_with_true |
5 | - r = WxPay::Result.new( | |
5 | + r = WxPay::Result[ | |
6 | 6 | Hash.from_xml( |
7 | 7 | <<-XML |
8 | 8 | <xml> |
... | ... | @@ -10,19 +10,38 @@ class WxPay::ResultTest < MiniTest::Test |
10 | 10 | <result_code>SUCCESS</result_code> |
11 | 11 | </xml> |
12 | 12 | XML |
13 | - )) | |
13 | + ) | |
14 | + ] | |
14 | 15 | |
15 | 16 | assert_equal r.success?, true |
16 | 17 | end |
17 | 18 | |
19 | + def test_success_method_with_true | |
20 | + r = WxPay::Result[ | |
21 | + Hash.from_xml( | |
22 | + <<-XML | |
23 | + <xml> | |
24 | + <return_code>SUCCESS</return_code> | |
25 | + <code_url>wx_code_url</code_url> | |
26 | + <result_code>SUCCESS</result_code> | |
27 | + </xml> | |
28 | + XML | |
29 | + ) | |
30 | + ] | |
31 | + | |
32 | + assert_equal r['return_code'].nil?, false | |
33 | + assert_equal r['prepay_id'].nil?, true | |
34 | + end | |
35 | + | |
18 | 36 | def test_success_method_with_false |
19 | - r = WxPay::Result.new( | |
37 | + r = WxPay::Result[ | |
20 | 38 | Hash.from_xml( |
21 | 39 | <<-XML |
22 | 40 | <xml> |
23 | 41 | </xml> |
24 | 42 | XML |
25 | - )) | |
43 | + ) | |
44 | + ] | |
26 | 45 | |
27 | 46 | assert_equal r.success?, false |
28 | 47 | end | ... | ... |