ZenVoich / test

Motoko testing library to run tests with mops
MIT License
5 stars 3 forks source link

Overwriting/mocking stable vars #9

Open jake-beardo opened 3 months ago

jake-beardo commented 3 months ago

I have an actor class which looks like this

actor class Orchestrator() = this {
    let { phash } = Map;

    private stable var pool = Map.new<Principal, Types.CanisterInfo>();

    let management = Management.service(Constants.ManagementCanisterId);

    // Auth so that only server and admin can talk to this canister
    let admin = Principal.fromText("qcjer-cl7l3-dae");
    let game_controller = Principal.fromText("7k7-lqlgk-x56ep");
    private stable var admins = Vector.fromArray<Principal>([admin, game_controller]);

I then have some tests which look like this

var orchestrator = await Orchestrator();

let caller : Principal = Principal.fromText("wo5qg-ysjiq-5da");

let user1 : Principal = Principal.fromText("kufey-x4r");
let user2 : Principal = Principal.fromText("mm6ly-2yowe");
let user3 : Principal = Principal.fromText("wkvns-5prta");
var admins : Vector<Principal> = Vector.fromArray([caller, user1, user2]);

func resetMocks() : () {
  admins := Vector.fromArray([caller, user1, user2]);
};

await suite(
  "createEvent",
  func() : async () {
    await test(
      "setEventWasm works correctly",
      func() : async () {
        resetMocks();

        let oldEventWasm = await orchestrator.getEventWasm();

The getEventWasm function requires admin verification so i need to change the admins in my Orchestrator actor class to include this principal wo5qg-ysjiq-5da. have you got a good way of suggesting how I do this? I could pass it as an argument to my actor orchestrator class when i create it i suppose but i was wondering if there might be a better way to do it

ZenVoich commented 3 months ago

Unfortunately there is no way to fake the caller principal id.

You can take a look at pocket-ic that might be more powerful to write e2e tests(and use rust, python or js): https://forum.dfinity.org/t/announcing-picjs-typescript-javascript-support-for-pocketic/24479 https://forum.dfinity.org/t/announcing-picjs-typescript-javascript-support-for-pocketic/24479