Let’s Write More Tests

Expand your testing suite to ensure comprehensive coverage of the smart contract.

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:

Press + to interact
// 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 with our transaction, it ...