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

[Feature] contract mocking #32

Closed
MarvinJanssen opened this issue Jun 2, 2021 · 6 comments
Closed

[Feature] contract mocking #32

MarvinJanssen opened this issue Jun 2, 2021 · 6 comments
Labels
functional enhancement New feature or request

Comments

@MarvinJanssen
Copy link

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.

@LNow
Copy link
Contributor

LNow commented Jul 3, 2021

I'm not convinced in 100% if we need a separate TS API for creating mocks.
For test purposes we can create various types of test doubles without any extra API.

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]);
  },
});

@MarvinJanssen
Copy link
Author

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 clarity/index.ts file.

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.

@obycode
Copy link
Member

obycode commented May 4, 2022

Are the needs in this issue handled by #333?

@moodmosaic
Copy link
Contributor

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.)

@lgalabru
Copy link
Contributor

I think these needs are handled in #333, although a better developer experience would be implemented with #357.

@lgalabru lgalabru added functional enhancement New feature or request and removed feature labels Aug 8, 2022
@lgalabru
Copy link
Contributor

lgalabru commented Oct 6, 2022

All the primitives are implemented and available to write such a library, I'll close this issue!

@lgalabru lgalabru closed this as completed Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
functional enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants