Motoko unit testing

I’m currently developing a canister and have been writing unit tests using asserts and whatnot, but is there a way to simulate calls to a canister from a particular principal?
The use case for this is so I can test permissions to call particular functions from different principals.
I’ve seen some examples using shell scripts, but this requires deploying the canister and is pretty slow to iterate on.

Any good solutions?


Check out GitHub - chenyan2002/ic-repl.

Not sure if you can change the calling principal in ic-repl scripting language, but worst case you can change your dfx identity in bash and somehow string it together.

I’m currently using a shell script to change dfx identity, but when I want to update something, it takes a couple of minutes to re-deploy canisters which isn’t ideal. I guess I can run the canister and then edit a shell script separately, but then I will have to use the same replica?

In ic-repl, you can use the identity command to switch caller identities. For example

identity alice;  // generates a random principal
call canister_id.method(); // caller is alice
identity bob "my_pem_file.pem";  // assign identity from "my_pem_file.pem" to bob
call canister_id.method(); // caller is bob
identity alice; // switch identity back to alice

Canister calls go through consensus, so it’s not fast. To test things end-to-end, we unfortunately have to tolerant this delay. For function level test, you can try to use matcher (GitHub - kritzcreek/motoko-matchers), which doesn’t require canister calls, so it’s much faster.

1 Like

Ah okay, interesting, thanks!

How do I install ic-repl cli?

You can download from the release page: Releases · chenyan2002/ic-repl · GitHub

1 Like

Got it! How do we obtain a .wasm file from a canister motoko file? I see you use a greet.wasm it in one of the examples.

After you run dfx build or dfx deploy, the Wasm file is at .dfx/local/canisters/

Oh right, thanks! Are you able to enable debug messages with ic-repl? Also, do you know if there’s a docker image for ic-repl?