I just finished following along with Project 3, which includes actions and blinks to the peanut butter voting project. At the very end, we had to anchor clean and anchor test to have it clear its cache and rebuild the program. We also included the following lines of code to set the provider and get the program
anchor.setProvider(anchor.AnchorProvider.env());
let votingProgram = anchor.workspace.Voting as Program<Voting>;
Finally had to replace the PublicKey with the program id of the deployed program.
Just For Reference: https://www.youtube.com/watch?v=amAq-WHAFs8&t=8989s&ab_channel=Solana at 2:29:50 / 11:08:04
Voting.spec.ts:
import * as anchor from '@coral-xyz/anchor'
import {Program} from '@coral-xyz/anchor'
import {PublicKey} from '@solana/web3.js'
import { startAnchor } from "solana-bankrun";
import { BankrunProvider } from "anchor-bankrun";
import {Voting} from '../target/types/voting'
const IDL = require('../target/idl/voting.json')
const votingAddress = new PublicKey("7tQxnjPywG85JEVTwKD4oiksY7y645ToPQFBBEcr1Fkp");
describe('voting', () => {
let context;
let provider;
anchor.setProvider(anchor.AnchorProvider.env());
let votingProgram = anchor.workspace.Voting as Program<Voting>;
beforeAll(async () => {
/*context = await startAnchor("", [{name: "voting", programId: votingAddress}], []);
provider = new BankrunProvider(context);
votingProgram = new Program<Voting>(
IDL,
provider,
); */
});
it('Initialize Poll', async () => {
await votingProgram.methods.initializePoll(
new anchor.BN(1),
"What is Your Fav type of Peanut Butter?",
new anchor.BN(0),
new anchor.BN(1929025852),
).rpc({skipPreflight: true});
const [pollAddress] = PublicKey.findProgramAddressSync(
[new anchor.BN(1).toArrayLike(Buffer, 'le', 8)],
votingAddress,
)
const poll = await votingProgram.account.poll.fetch(pollAddress);
console.log(poll);
expect(poll.pollId.toNumber()).toEqual(1);
expect(poll.pollDescription).toEqual("What is Your Fav type of Peanut Butter?");
expect(poll.pollStartTime.toNumber()).toBeLessThan(poll.pollEndTime.toNumber());
});
it('initialize candidate', async ()=> {
await votingProgram.methods.initializeCandidate(
"Smooth",
new anchor.BN(1),
).rpc({skipPreflight: true});
await votingProgram.methods.initializeCandidate(
"Crunchy",
new anchor.BN(1),
).rpc({skipPreflight: true});
const [crunchyAddress] = PublicKey.findProgramAddressSync(
[new anchor.BN(1).toArrayLike(Buffer, 'le', 8), Buffer.from("Crunchy")],
votingAddress,
);
const [smoothAddress] = PublicKey.findProgramAddressSync(
[new anchor.BN(1).toArrayLike(Buffer, 'le', 8), Buffer.from("Smooth")],
votingAddress,
);
const crunchyCandidate = await votingProgram.account.candidate.fetch(crunchyAddress);
const smoothyCandidate = await votingProgram.account.candidate.fetch(smoothAddress);
console.log(crunchyCandidate);
console.log(smoothyCandidate);
expect(crunchyCandidate.candidateVotes.toNumber()).toEqual(0);
expect(smoothyCandidate.candidateVotes.toNumber()).toEqual(0);
})
it('vote', async () => {
await votingProgram.methods.vote(
"Smooth",
new anchor.BN(1),
).rpc({skipPreflight: true});
const [smoothAddress] = PublicKey.findProgramAddressSync(
[new anchor.BN(1).toArrayLike(Buffer, 'le', 8), Buffer.from("Smooth")],
votingAddress,
);
const smoothyCandidate = await votingProgram.account.candidate.fetch(smoothAddress);
console.log(smoothyCandidate);
expect(smoothyCandidate.candidateVotes.toNumber()).toEqual(1);
});
});
I guess I am not sure. but whenever I run solana-test-validator It says it has been started on
but on https://explorer.solana.com/ for http://localhost:8899 it does not detect the network.
But https://explorer.solana.com/ only at http://127.0.0.0:8899
My Terminal Log after Running: anchor test --skip-local-validator
simultaneously running: solana-test-validator
Deploy success
Found a 'test' script in the Anchor.toml. Running it as a test suite!
Running test suite: "/home/harsh/Documents/sol-temp/voting-dapp/anchor/Anchor.toml"
(node:11238) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
FAIL tests/voting.spec.ts (7.854 s)
voting
✕ Initialize Poll (204 ms)
✕ initialize candidate (413 ms)
✕ vote (384 ms)
● voting › Initialize Poll
at Function.parse (node_modules/@coral-xyz/anchor/src/error.ts:250:14)
at translateError (node_modules/@coral-xyz/anchor/src/error.ts:282:37)
at MethodsBuilder.rpc [as _rpcFn] (node_modules/@coral-xyz/anchor/src/program/namespace/rpc.ts:35:29)
● voting › initialize candidate
at Function.parse (node_modules/@coral-xyz/anchor/src/error.ts:250:14)
at translateError (node_modules/@coral-xyz/anchor/src/error.ts:282:37)
at MethodsBuilder.rpc [as _rpcFn] (node_modules/@coral-xyz/anchor/src/program/namespace/rpc.ts:35:29)
● voting › vote
at Function.parse (node_modules/@coral-xyz/anchor/src/error.ts:250:14)
at translateError (node_modules/@coral-xyz/anchor/src/error.ts:282:37)
at MethodsBuilder.rpc [as _rpcFn] (node_modules/@coral-xyz/anchor/src/program/namespace/rpc.ts:35:29)
Test Suites: 1 failed, 1 total
Tests: 3 failed, 3 total
Snapshots: 0 total
Time: 7.887 s, estimated 18 s
Ran all test suites.