As I was going through the truffle test for the smart contract below to check if it has the owner address:
pragma solidity >=0.4.21 <0.7.0;
contract Owner {
address public owner;
address public nextOwner;
constructor () public {
owner = msg.sender;
}
}
it fails with the message below: after running the truffle test.
Using network 'development'.
Compiling your contracts...
===========================
> Compiling ./contracts/Owner.sol
> Artifacts written to /tmp/test-2020319-8538-1ih1ur3.6ijj
> Compiled successfully using:
- solc: 0.5.16+commit.9c3226ce.Emscripten.clang
Contract: Owner
The attributes
0x7C94D330943a4a6CC1c25eF043446dA1898F4688
✓ has the address
1) has the owner
> No events were emitted
1 passing (406ms)
1 failing
1) Contract: Owner
The attributes
has the owner:
has owner address
+ expected - actual
-0x0000000000000000000000000000000000000000
+0xBcB8563A1f9707BDa6bb020222b664Ddc100e5a4
at Context.it (test/DIce.js:21:20)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
It is clear that the actual and the expected was not equal.
The test script I was using is below:
const Owner = artifacts.require('./Owner.sol');
contract('Owner', accounts => {
const owner = accounts[0];
const nextOwner = accounts[1];
describe('The attributes', () => {
let ownerInstance;
beforeEach(async () => {
ownerInstance = await Owner.new();
});
it('has the address', () => {
const address = ownerInstance.address;
assert.notEqual(address, '0x0', 'has contract address');
console.log(address);
});
it('has the owner', async () => {
const address = await ownerInstance.nextOwner();
assert.equal(address, owner, 'has owner address');
});
});
});
Could you please advice? I would appreciate discussions over this. Thanks.
assert.notEqual(address, '0x0', 'has contract address');
, because the zero address is the string'0x'
followed by 40'0'
s, and this test will therefore always complete successfully, even when the contract doesn't yet have an address (so this test will not serve its purpose). – goodvibration Apr 19 '20 at 14:44assert.notEqual(address, '0x0', 'has contract address');
return the exact contract address here. and As per your recommendation, I used the below:assert(address != "0x" + "0".repeat(40),'has contract address');
, it passed. Thanks – Aldo Apr 19 '20 at 14:58