I am attempting to write some unit tests as I'm working on the development of some smart contracts, but I've run into an issue that's preventing me from continuing. I'm attempting to check the address of a contract instance that has been imported into my smart contract, which has been deployed to my local network.
For example:
import "./ContractB.sol";
contract ContractA {
ContractB public _cB;
constructor(address _addr) public {
_cB = ContractB(_addr);
}
}
I would like to retrieve the address of _cB after ContractA has been deployed to the network, but my attempts have been unsuccessful thus far. Here are the methods I've tried to extract the address, but none have proven successful so far:
First Method:
contract('ContractA', (accounts) => {
it("should return the address of the ContractB contract", () => {
return ContractA.deployed().then((instance) => {
return instance._cB;
}).then((address) => {
assert.equal(address, accounts[0], "The addresses don't match");
});
});
});
Second Method:
contract('ContractA', (accounts) => {
it("should return the address of the ContractB contract", () => {
return ContractA.deployed().then((instance) => {
return instance._cB.address;
}).then((address) => {
assert.equal(address, accounts[0], "The addresses don't match");
});
});
});
Third Method:
contract('ContractA', (accounts) => {
it("should return the address of the ContractB contract", () => {
return ContractA.deployed().then((instance) => {
return instance._cB.call();
}).then((address) => {
assert.equal(address, accounts[0], "The addresses don't match");
});
});
});
Fourth Attempt
var contractA = null;
beforeEach(() => {
return ContractA.new(accounts[0]).then((instance) => {
contractA = instance;
})
});
it("should return the address of the ContractB contract", () => {
assert.equal(contractA._cB.address, accounts[0], "The addresses don't match");
});
If anyone has any ideas or has had experience with this, I would appreciate the help. Thanks in advance.
P.S I know the test-case that I've written will always return false, but I'd just like to see an output. At the moment, I always get "undefined".