如何在 NEAR 智能合约单元测试(网络组装)中更改 Context.sender?

How to change Context.sender in a NEAR Smart Contract unit test (web assembly)?

我有一个函数,addWord(word:string),它禁止同一个Context.sender添加多个连续的单词。我将最后一个签名者存储在合同中。如果下面的代码中有一些语法错误,请原谅我,我只添加了我当前代码的一部分。

export class Contract {
 private lastSignedBy: string = '';
 private words: PersistentVector<string> = new PersistentVector<string>('w');

 @mutateState()
 addWord(word: string): number {
  // can't add more than one consecutive word
  if(this.lastSignedBy != Context.sender){
    this.words.push(word);
    this.lastSignedBy = Context.sender;
    return 1;
  }
  return 0; 
 }
}

现在,我想测试这个功能,但我不知道如何在测试中更改发件人。我想也许我需要模拟 Context,但我不确定该怎么做。测试框架是as-pect

这是我当前的测试。

let contract: Contract;
    
beforeEach(() => {
  contract = new Contract();
});
    
describe('Contract', () => {
  it('can add word if different sender than last time', () => {
    expect(contract.addWord('word one')).toStrictEqual(1);
    // TODO change Context.sender somehow. 
    expect(contract.addWord('second word')).toStrictEqual(1);
  });
}

您可以像这样使用对 VMContext 的引用

import { VMContext } from "near-sdk-as";
...

it("provides access to most recent player", () => {
  VMContext.setSigner_account_id('player1')
});

https://github.com/Learn-NEAR/NCD.L1.sample--lottery/blob/2bd11bc1092004409e32b75736f78adee821f35b/src/lottery/__tests__/index.unit.spec.ts#L68