'Failing tests -> Cannot read properties of undefined (reading 'rpc')
I was writing a test in TypeScript for Adding two numbers. 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. Can someone help?
Solution 1:[1]
program
is undefined. That means this line of code has issue
const program = Anchor.workspace.calculator;
Anchor capitalizes the contract name. so it should be "Calculator"
const program = Anchor.workspace.Calculator;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | 0xLogN |