Search⌘ K
AI Features

Let’s Write More Tests

Explore how to write comprehensive tests for Solidity smart contracts, including creating and verifying creator and applicant profiles, job postings, and job applications. Understand account context switching and key Solidity concepts like msg.sender and tx.origin to ensure reliable contract behavior. Practice extending tests to cover application approval and rejection to deepen your development skills in building secure Ethereum dApps.

Let’s continue by writing more test cases. Specifically, we’ll test creator and applicant profile creation, job creation, and application. For practice, we recommend testing further for application approval and rejection.

Writing the tests

Let’s write a test to check how our creator profile creation works.

Testing the creator profile function

Copy and paste the test case code below into your project:

Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "remix_tests.sol"; // this import is automatically injected by Remix.
import "hardhat/console.sol";
import "remix_accounts.sol";
import "../contracts/SolJobs.sol";
contract SolJobsTest {
SolJobs solJobs;
/// #sender: account-0
function beforeAll() public {
solJobs = new SolJobs();
}
function testInitialValues() public {
console.log("Testing the initial values of our state count variables");
Assert.equal(solJobs.getNumberOfCreatorProfiles(), 0, "number of creator profiles should be 0");
Assert.equal(solJobs.getNumberOfApplicantProfiles(), 0, "number of applicant profiles should be 0");
Assert.equal(solJobs.getNumberOfJobsCreated(), 0, "number of jobs created should be 0");
Assert.equal(solJobs.getNumberOfApplications(), 0, "number of applications submitted should be 0");
}
/// #sender: account-1
function testCreatorProfile() public {
solJobs.createCreatorProfile("Test Name", "test@email.com", "Test description", "Test tagline");
Assert.equal(solJobs.getNumberOfCreatorProfiles(), 1, "number of creator profiles should be 1");
}
}
  • Line 6: We import remix_accounts.sol, a library to help us switch account contexts.

  • Line 12: We introduce a custom transaction context for the sender account used when initializing the contract. We use the first account as the manager. Currently, we can only set custom context values for sender and value. We do this using the NatSpec comment format: Three forward slashes (///) followed by the parameter key prefixed with a hash (#), ending with a colon and space (: ). For example, if we want to send a value of 20 ...