[英]Failing tests -> Cannot read properties of undefined (reading 'rpc')
[英]Failing tests: cannot read properties of undefined(reading local) solana devnet
您好,我正在嘗試在錨點中測試我的第一個文件,但它一直出現此錯誤,我得到的錯誤是:
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 文件進行測試:
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');
});
});
我已經看到另一個類似的關於無法讀取 rpc 的 stackoverflow postv,但也沒有得到回答:(
所以我必須從 @project-serum/anchor 導入 AnchorProvider 和 web3,除了導入 * 作為錨點。 然后我們將提供程序設置為const provider = AnchorProvider.local()
還將const {SystemProgram } = anchor.web3
為const {SystemProgram } = web3
並更改帳戶內的 system_program > create rpc to systemProgram ,瞧我們的測試工作! 完整的測試代碼:
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');
});
});
我認為您代碼中的主要問題是您的 dapp 的名稱。
const program = anchor.workspace.mycalcdapp;
你的工作區名稱應該用大寫字母初始化
const program = anchor.workspace.Mycalcdapp;
同樣在“it”塊中welcome
拼寫錯誤
await program.rpc.create('Welocme to solana', {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.