-
Notifications
You must be signed in to change notification settings - Fork 137
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
[Feature] contract mocking #32
Comments
I'm not convinced in 100% if we need a separate TS API for creating mocks. Example: Clarinet.test({
name: "returns the ticket price",
preSetup(): Tx[] {
let mockOracleCode = `
(define-public (price (arg1 (string-ascii 24)) (arg2 (string-ascii 24)) (arg3 uint))
(ok u2000000)
)
`;
return [
Tx.deployContract(
"imaginary-oracle-v1",
mockOracleCode,
"SP000000000000000000002Q6VF78"
),
];
},
async fn(chain: Chain, accounts: Map<string, Account>) {
let deployer = accounts.get("deployer")!;
let wallet_1 = accounts.get("wallet_1")!;
let block = chain.mineBlock([
Tx.contractCall(
`SP000000000000000000002Q6VF78.imaginary-oracle-v1`,
"price",
[types.ascii("usd"), types.ascii("stx"), types.uint(5)],
wallet_1.address
),
]);
console.table(block.receipts[0]);
},
}); |
Sure, we can do that, just like we can also construct transactions manually. A library simply makes it easier. It definitely does not have to (should not?) be part of the built-in Furthermore, I do not like the idea of mixing languages so I rather not write Clarity snippets in TS files unless I absolutely have to. Having an exposed API gives us more type safety. With the new Clarinet features coming out, we might want to explore different approaches as well. |
Are the needs in this issue handled by #333? |
Partially, or perhaps implicitly.
(If we move forward with this, the correct terminology matters, as in this snippet the Test Double is actually a Stub, not a Mock.) |
All the primitives are implemented and available to write such a library, I'll close this issue! |
Many projects use external / 3rd party contracts. Fetching them from mainnet or testnet is not always ideal, nor would you want code that is not part of your project in your code base. Furthermore, sometimes you want to be able to exactly control outputs. I think Clarinet should have the ability to mock contracts.
This can take the form of an extension with a pure TS implementation, so only people that need it can bring it in.
Only minimal changes to Clarinet itself are required (of which preSetup was the first.) It mainly has to do with analysis. We would also have to think about how to deal with mocks when invoking
clarinet console
.Here is an early implementation:
https://github.com/MarvinJanssen/clarinet/tree/feat/mock-contracts
Sample of how it could work:
https://github.com/MarvinJanssen/clarinet/tree/feat/mock-contracts/examples/mocking
However, one normally wants to be able to mock contract per unit test.
The text was updated successfully, but these errors were encountered: