1

I was trying to mock the rpc calls in substrate, but if there's any example for that then I guess it would help better.

1 Answer 1

1

You can look here:

For example:

#[tokio::test]
async fn should_return_header() {
    let client = Arc::new(substrate_test_runtime_client::new());
    let api = new_full(client.clone(), test_executor()).into_rpc();

    let res: Header =
        api.call("chain_getHeader", [H256::from(client.genesis_hash())]).await.unwrap();
    assert_eq!(
        res,
        Header {
            parent_hash: H256::from_low_u64_be(0),
            number: 0,
            state_root: res.state_root,
            extrinsics_root: "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314"
                .parse()
                .unwrap(),
            digest: Default::default(),
        }
    );

    let res: Header = api.call("chain_getHeader", EmptyParams::new()).await.unwrap();
    assert_eq!(
        res,
        Header {
            parent_hash: H256::from_low_u64_be(0),
            number: 0,
            state_root: res.state_root,
            extrinsics_root: "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314"
                .parse()
                .unwrap(),
            digest: Default::default(),
        }
    );

    assert_matches!(
        api.call::<_, Option<Header>>("chain_getHeader", [H256::from_low_u64_be(5)])
            .await
            .unwrap(),
        None
    );
}

There are different test setups for RPC. You can poke around in the substrate/client/rpc/src folder:

1
  • I will take a look here. Thanks
    – kroos47
    Sep 27, 2022 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.