In the previous lesson, we completed our proposal smart contract and finalized the Candid interface. In this session, we focus on testing the contract to ensure all functions behave as expected before integrating with the frontend.
We begin by starting the Internet Computer local development environment with:
dfx start --background
Once the server is active, we deploy our smart contract:
dfx deploy
This step may take a few seconds.
We use the UI provided by DFX to test our contract functions. To start, we create a new proposal:
The contract returns null because, as designed, the create_proposal function does not return any data when a new proposal is created. Since this is the first entry, no prior proposal exists to return.
We validate the proposal structure:
Everything is working as intended.
We call the function to retrieve the total number of proposals. The output is:
Proposal Count: 1
This confirms the first proposal was registered correctly.
We attempt to edit a non-existent proposal (ID: 2). The system correctly returns:
Error: Variant(NoSuchProposal)
We then edit the first proposal:
The function returns OK. Fetching the proposal again confirms the changes were applied.
We test the voting options: approve, reject, and pass.
We submit a pass vote for the first proposal, and the vote is recorded:
Attempting to vote a second time from the same principal results in:
Error: Already Voted
This confirms that the smart contract correctly prevents duplicate voting.
We test the end_proposal function on the first proposal. Once called, the proposal becomes inactive:
Subsequent voting is disabled for this proposal.
We create a second proposal titled "Second Proposal" and verify:
All backend functions for our proposal smart contract are tested and working as expected:
We’re now ready to build the frontend interface to interact with this contract and complete our decentralized application.
Swap insights and ask questions about “Build on Internet Computer with ICP Rust CDK”.
Ask a question or share your thoughts about this lesson.