Rise In Logo

Build on Internet Computer with ICP Rust CDK

Testing and Deploying Smart Contract

Testing and Deploying Smart Contract

Testing the Proposal Smart Contract on the Internet Computer

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.

Step 1: Starting the Local Development Server

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.

Step 2: Verifying the Create Proposal Function

We use the UI provided by DFX to test our contract functions. To start, we create a new proposal:

  • Description: "First Proposal"
  • Active: true

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:

  • approve, reject, and pass counts are zero
  • owner is set to the caller’s principal.
  • voters list is empty.
  • is_active is true.

Everything is working as intended.

Step 3: Checking Proposal Count

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.

Step 4: Testing Edit Function and Error Handling

We attempt to edit a non-existent proposal (ID: 2). The system correctly returns:

Error: Variant(NoSuchProposal)

We then edit the first proposal:

  • New description: "First Proposal Edited"
  • is_active: true

The function returns OK. Fetching the proposal again confirms the changes were applied.

Step 5: Voting Functionality

We test the voting options: approve, reject, and pass.

We submit a pass vote for the first proposal, and the vote is recorded:

  • pass: 1
  • approve: 0
  • reject: 0

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.

Step 6: Ending a Proposal

We test the end_proposal function on the first proposal. Once called, the proposal becomes inactive:

  • is_active: false

Subsequent voting is disabled for this proposal.

Step 7: Creating and Testing Another Proposal

We create a second proposal titled "Second Proposal" and verify:

  • Proposal count increases to 2
  • The second proposal is retrievable and correctly registered

Conclusion and Next Steps

All backend functions for our proposal smart contract are tested and working as expected:

  • Creating, editing, and retrieving proposals
  • Voting with validation
  • Managing state transitions like ending a proposal

We’re now ready to build the frontend interface to interact with this contract and complete our decentralized application.

Comments

You need to enroll in the course to be able to comment!

Stay in the know

Never miss updates on new programs and opportunities.

Rise In Logo

Rise together in web3!

Disclaimer: The information /programs / events provided on https://patika.dev and https://risein.com are strictly for upskilling and networking purposes related to the technical infrastructure of blockchain platforms. We do not provide financial or investment advice and do not make any representations regarding the value, profitability, or future price of any blockchain or cryptocurrency. Users are encouraged to conduct their own research and consult with licensed financial professionals before engaging in any investment activities. https://patika.dev and https://risein.com disclaim any responsibility for financial decisions made by users based on information provided here.