Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Testing framework] Mocking framework #449

Open
sisyphusSmiling opened this issue Nov 4, 2024 · 0 comments
Open

[Testing framework] Mocking framework #449

sisyphusSmiling opened this issue Nov 4, 2024 · 0 comments

Comments

@sisyphusSmiling
Copy link

sisyphusSmiling commented Nov 4, 2024

Issue to be solved

There may be instances when mocking responses would be very useful. Currently, this requires some creative interface design specifically for the purposes of mocking as the functionality isn't supported natively.

Suggested Solution

I'm not clear on the implementation details required for this feature, but I'll leave some practical interface options for consideration and discussion

import "Foo"

access(all)
fun testBar() {
  let expected: Int = 1

  // Label the subsequent call to Foo.bar() as mocked and return the provided value
  Test.mock(Foo.bar(), expected)
  let res = Foo.bar

  Test.assertEqual(expected, res)
}

I'd also assume that implementation details for fvm-native functionality might look different. For instance, mocking calls to CadenceArch would be helpful for devs using Cadence in the EVM context

import CadenceArch // Assuming some mechanism for statically declaring CadenceArch calls from Cadence
import "EVM"

access(all)
fun testRevertibleRandom() {
  let expected: UInt64 = 1

  // Label the subsequent call to CadenceArch precompile as mocked and return the provided value
  Test.mock(CadenceArch.revertibleRandom(), expected)
  let res = executeScript("...", []) // execute some script that gets EVM randomness from a contract in EVM

  Test.assertEqual(expected, res.returnValue!)
}

In general, testing randomness in either Cadence or EVM is rather difficult given the inherent non-deterministic behavior, and mocking would be helpful especially for this use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant