[英]Failing tests: cannot read properties of undefined(reading local) solana devnet
Hello i am trying to test my first file in anchor and it keeps giving this error, error i got:您好,我正在尝试在锚点中测试我的第一个文件,但它一直出现此错误,我得到的错误是:
TypeError: Cannot read properties of undefined (reading 'local')
at Suite.<anonymous> (/home/benny/mycalcdapp/tests/mycalcdapp.ts:6:36)
at Object.create (/home/benny/mycalcdapp/node_modules/mocha/lib/interfaces/common.js:148:19)
at context.describe.context.context (/home/benny/mycalcdapp/node_modules/mocha/lib/interfaces/bdd.js:42:27)
at Object.<anonymous> (/home/benny/mycalcdapp/tests/mycalcdapp.ts:5:1)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Module.m._compile (/home/benny/mycalcdapp/node_modules/ts-node/src/index.ts:439:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
.ts file for test: .ts 文件进行测试:
const assert = require('assert');
const anchor = require('@project-serum/anchor');
const { SystemProgram } = anchor.web3;
describe('mycalcdapp', () => {
const provider = anchor.Provider.local();
anchor.setProvider(provider);
const calculator = anchor.web3.Keypair.generate();
const program = anchor.workspace.mycalcdapp;
it('creates a calculator', async () => {
await program.rpc.create('Welocme to solana', {
accounts: {
calculator: calculator.publicKey,
user: provider.wallet.publicKey,
system_program: SystemProgram.programId,
},
signers: [calculator],
});
const account = await program.account.calculator.fetch(
calculator.publicKey
);
assert.ok(account.greeting === 'Welcome to solana');
});
});
i have seen another similar stackoverflow postv about not being able to read rpc but it was not answered either:(我已经看到另一个类似的关于无法读取 rpc 的 stackoverflow postv,但也没有得到回答:(
so I had to import AnchorProvider and web3 from @project-serum/anchor apart from importing * as anchor.所以我必须从 @project-serum/anchor 导入 AnchorProvider 和 web3,除了导入 * 作为锚点。 then we set the provider as
const provider = AnchorProvider.local()
also change const {SystemProgram } = anchor.web3
to const {SystemProgram } = web3
and change system_program inside accounts > create rpc to systemProgram and voila our test works!!然后我们将提供程序设置为
const provider = AnchorProvider.local()
还将const {SystemProgram } = anchor.web3
为const {SystemProgram } = web3
并更改帐户内的 system_program > create rpc to systemProgram ,瞧我们的测试工作! full test code:完整的测试代码:
import assert from 'assert';
import * as anchor from '@project-serum/anchor';
import { AnchorProvider, web3 } from '@project-serum/anchor';
const { SystemProgram } = web3;
describe('mycalcdapp', () => {
const provider = AnchorProvider.local();
anchor.setProvider(provider);
const calculator = anchor.web3.Keypair.generate();
const program = anchor.workspace.Mycalcdapp;
it('creates a calculator', async () => {
await program.rpc.create('Welcome to solana', {
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 solana');
});
});
I think the main issue in your code was the name of your dapp.我认为您代码中的主要问题是您的 dapp 的名称。
const program = anchor.workspace.mycalcdapp;
your workspace name should be initialized with capital letter你的工作区名称应该用大写字母初始化
const program = anchor.workspace.Mycalcdapp;
also inside "it" block welcome
is misspelled同样在“it”块中
welcome
拼写错误
await program.rpc.create('Welocme to solana', {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.