[英]Failing tests -> Cannot read properties of undefined (reading 'rpc')
I was writing a test in TypeScript for Adding two numbers.我正在 TypeScript 中编写一个添加两个数字的测试。 I am receiving the following error:
我收到以下错误:
Adds two numbers:
TypeError: Cannot read properties of undefined (reading 'rpc')
at Context.<anonymous> (tests/calculator.ts:33:19)
at Generator.next (<anonymous>)
at /mnt/e/solana/calculator/tests/calculator.ts:7:71
at new Promise (<anonymous>)
at __awaiter (tests/calculator.ts:3:12)
at Context.<anonymous> (tests/calculator.ts:35:16)
at processImmediate (node:internal/timers:473:21)
Following is the code:以下是代码:
const Anchor = require('@project-serum/anchor');
describe('calculator',() => {
const provider = Anchor.Provider.local();
Anchor.setProvider(provider);
const calculator = Anchor.web3.Keypair.generate();
const program = Anchor.workspace.calculator;
var _calculator;
it('Creates a calculator', async() => {
await program.rpc.create("Welcome to My calculator",{
accounts: {
calculator: calculator.publicKey,
user: provider.wallet.publicKey,
SystemProgram: SystemProgram.programId,
},
signers: [calculator]
});
const account = await
program.account.calculator.fetch(calculator.publicKey);
assert.ok(account.greeting === "Welcome to My calculator");
_calculator = calculator;
});
This is working on Solana blockchain using anchor-cli.这是使用 anchor-cli 在 Solana 区块链上工作的。 Can someone help?
有人可以帮忙吗?
program
is undefined. program
未定义。 That means this line of code has issue这意味着这行代码有问题
const program = Anchor.workspace.calculator;
Anchor capitalizes the contract name. Anchor 将合约名称大写。 so it should be "Calculator"
所以它应该是“计算器”
const program = Anchor.workspace.Calculator;
import AnchorProvider from @project-serum/anchor and assign provider to it instead, like:从 @project-serum/anchor 导入 AnchorProvider 并将提供者分配给它,例如:
const provider = AnchorProvider.local()
I think this should solve the problem.我认为这应该可以解决问题。 If you have an error with systemProgram, import web3 from anchor in a similar manner.
如果 systemProgram 出错,请以类似方式从锚点导入 web3。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.