diff --git a/test/models/cita_sync/api_test.rb b/test/models/cita_sync/api_test.rb index da73da7..860054e 100644 --- a/test/models/cita_sync/api_test.rb +++ b/test/models/cita_sync/api_test.rb @@ -2,10 +2,7 @@ class CitaSync::ApiTest < ActiveSupport::TestCase setup do - stub_request(:post, "www.cita.com"). - with(body: hash_including({ method: "blockNumber" }), headers: { "Content-Type": "application/json" }). - to_return(status: 200, body: { jsonrpc: "2.0", id: 83, result: "0x7781" }.to_json) - + stub_request_wrapper("blockNumber", nil, "0x7781") result = { "version" => 0, @@ -25,10 +22,8 @@ class CitaSync::ApiTest < ActiveSupport::TestCase "transactions" => [] } } - stub_request(:post, "www.cita.com"). - with(body: hash_including({ method: "getBlockByNumber", params: ["0x1", true] }), headers: { "Content-Type": "application/json" }). - to_return(status: 200, body: { jsonrpc: "2.0", id: 83, result: result }.to_json) + stub_request_wrapper("getBlockByNumber", ["0x1", true], result) end test "cita_blockNumber" do diff --git a/test/models/cita_sync/persist_test.rb b/test/models/cita_sync/persist_test.rb index 89ea0a5..79b770f 100644 --- a/test/models/cita_sync/persist_test.rb +++ b/test/models/cita_sync/persist_test.rb @@ -2,9 +2,7 @@ class CitaSync::ApiTest < ActiveSupport::TestCase def mock_block_number - stub_request(:post, "www.cita.com"). - with(body: hash_including({ method: "blockNumber" }), headers: { "Content-Type": "application/json" }). - to_return(status: 200, body: { jsonrpc: "2.0", id: 83, result: "0x1" }.to_json) + stub_request_wrapper("blockNumber", nil, "0x1") end def mock_get_block_by_number_zero @@ -27,9 +25,7 @@ def mock_get_block_by_number_zero } } - stub_request(:post, "www.cita.com"). - with(body: hash_including({ method: "getBlockByNumber", params: ["0x0", true] }), headers: { "Content-Type": "application/json" }). - to_return(status: 200, body: { jsonrpc: "2.0", id: 83, result: result }.to_json) + stub_request_wrapper("getBlockByNumber", ["0x0", true], result) end def mock_get_block_by_number_one @@ -56,9 +52,8 @@ def mock_get_block_by_number_one ] } } - stub_request(:post, "www.cita.com"). - with(body: hash_including({ method: "getBlockByNumber", params: ["0x1", true] }), headers: { "Content-Type": "application/json" }). - to_return(status: 200, body: { jsonrpc: "2.0", id: 83, result: result }.to_json) + + stub_request_wrapper("getBlockByNumber", ["0x1", true], result) end def mock_get_transaction @@ -70,9 +65,7 @@ def mock_get_transaction "index": "0x0" } - stub_request(:post, "www.cita.com"). - with(body: hash_including({ method: "getTransaction", params: ["0xee969624a87a51fc4acc958a3bb83ca32539ee54ebb4215668fe1029eeab59d4"] }), headers: { "Content-Type": "application/json" }). - to_return(status: 200, body: { jsonrpc: "2.0", id: 83, result: result }.to_json) + stub_request_wrapper("getTransaction", ["0xee969624a87a51fc4acc958a3bb83ca32539ee54ebb4215668fe1029eeab59d4"], result) end def mock_get_meta_data @@ -94,19 +87,13 @@ def mock_get_meta_data "website": "https://www.example.com" } - stub_request(:post, "www.cita.com"). - with(body: hash_including({ method: "getMetaData", params: ["0x0"] }), headers: { "Content-Type": "application/json" }). - to_return(status: 200, body: { jsonrpc: "2.0", id: 83, result: result }.to_json) + stub_request_wrapper("getMetaData", ["0x0"], result) end def mock_get_balance result = "0x0" - stub_request(:post, "www.cita.com"). - with(body: hash_including({ method: "getBalance", params: ["0x0dcf740686de1fe9e9faa4b519767a872e1cf69e", "0x0"] }), headers: { - "Content-Type": "application/json" - }). - to_return(status: 200, body: { jsonrpc: "2.0", id: 83, result: result }.to_json) + stub_request_wrapper("getBalance", ["0x0dcf740686de1fe9e9faa4b519767a872e1cf69e", "0x0"], result) end setup do diff --git a/test/test_helper.rb b/test/test_helper.rb index 3455d35..a9b5078 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,3 +9,20 @@ class ActiveSupport::TestCase # Add more helper methods to be used by all tests here... end + +module MockHelper + def stub_request_wrapper(method, params, result) + include_hash = if params.nil? + { method: method } + else + { method: method, params: params } + end + stub_request(:post, "www.cita.com"). + with(body: hash_including(include_hash), headers: { "Content-Type": "application/json" }). + to_return(status: 200, body: { jsonrpc: "2.0", id: 83, result: result }.to_json) + end +end + +class ActiveSupport::TestCase + include MockHelper +end