Testing the Auction Smart Contract
Learn how to write advanced tests using the Truffle Framework, and implement tests for the Auction smart contract
We'll cover the following...
In this lesson, we'll dive deeper into writing unit tests with Truffle and practice our new skills by writing tests for the Auction
contract. This contract is more complex and will allow us to learn how to write more advanced tests.
Initialize the contract
To create tests for the new contract, we'll add another test file called auction.js
in the tests
.
test├── auction.js└── ownable.js
There are no requirements to have different files to test different contracts, but this separation would make it easier to work with those tests.
To test the Auction
contract, we'll have a slightly more complex setup. For each auction, we first need to create an Ownable
instance, and then create an Auction
instance using it. To do this, we begin by defining two variables, one for the Ownable
contract instance and another for the Auction
contract. We then initialize both of them in the beforeEach
method.
const Ownable = artifacts.require('Ownable')const Auction = artifacts.require('Auction')contract('Auction', function (accounts) {let ownablelet auctionconst beneficiary = accounts[0]beforeEach(async function () {ownable = await Ownable.new('ticket', { from: beneficiary })auction = await Auction.new(ownable.address, { from: beneficiary })})...})
We also arbitrarily select the first account in the accounts
list as the auction’s beneficiary.