Skip to content

Commit

Permalink
wrapper stub request for rpc call
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Jul 4, 2018
1 parent 8242e3f commit 0de818f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
9 changes: 2 additions & 7 deletions test/models/cita_sync/api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
27 changes: 7 additions & 20 deletions test/models/cita_sync/persist_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0de818f

Please sign in to comment.