When we run truffle test
it deploy new contracts using migrations. It doesn’t use existing deployments (unless we tell the deployer not to).
To use the existing deployed contracts we need to use overwrite: false
when calling deploy
in our migrations scripts.
See Truffle documentation for details: https://www.trufflesuite.com/docs/truffle/getting-started/running-migrations#deployer-deploy-contract-args-options-
// 2_deploy.js
const Box = artifacts.require("Box");
module.exports = async function (deployer) {
await deployer.deploy(Box, {overwrite: false});
this.deployedBox = await Box.deployed();
console.log(`Migrations deployed Box: ${this.deployedBox.address}`);
};
// Box.test.js
// test/Box.test.js
// Load dependencies
const { expect } = require('chai');
// Load compiled artifacts
const Box = artifacts.require('Box');
// Start test block
contract('Box', function () {
beforeEach(async function () {
// Deploy a new Box contract for each test
this.deployedBox = await Box.deployed();
console.log(`Deployed Box: ${this.deployedBox.address}`);
});
// Test case
it('retrieve returns a value previously stored', async function () {
// Store a value
await this.deployedBox.store(42);
// Test if the returned value is the same one
// Note that we need to use strings to compare the 256 bit integers
expect((await this.deployedBox.retrieve()).toString()).to.equal('42');
});
});
Deploy
$ npx truffle migrate
...
Starting migrations...
======================
> Network name: 'development'
> Network id: 1605164995484
> Block gas limit: 6721975 (0x6691b7)
1_initial_migration.js
======================
...
2_deploy.js
===========
Deploying 'Box'
---------------
> transaction hash: 0x770ac33ee180217983cf64fc8f85dea37ec9e21fd0157abbcbc142b3ae9b6fb9
> Blocks: 0 Seconds: 0
> contract address: 0xb09bCc172050fBd4562da8b229Cf3E45Dc3045A6
> block number: 21
> block timestamp: 1605166801
> account: 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
> balance: 99.96235378
> gas used: 108291 (0x1a703)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00216582 ETH
Migrations deployed Box: 0xb09bCc172050fBd4562da8b229Cf3E45Dc3045A6
...
Testing
When running a test the existing deployment is used. 0xb09bCc172050fBd4562da8b229Cf3E45Dc3045A6
$ npx truffle test
Using network 'development'.
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Migrations deployed Box: 0xb09bCc172050fBd4562da8b229Cf3E45Dc3045A6
Contract: Box
Deployed Box: 0xb09bCc172050fBd4562da8b229Cf3E45Dc3045A6
✓ retrieve returns a value previously stored (100ms)
1 passing (163ms)
Testing with overwrite: true
in migrations
A new contract is deployed by migrations when testing.
$ npx truffle test
Using network 'development'.
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Migrations deployed Box: 0x5017A545b09ab9a30499DE7F431DF0855bCb7275
Contract: Box
Deployed Box: 0x5017A545b09ab9a30499DE7F431DF0855bCb7275
✓ retrieve returns a value previously stored (100ms)
1 passing (171ms)