[Testing framework] Mocking framework
Opened this issue · 0 comments
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.