1

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".

1 Answer 1

0

Apologies if I flub the syntax.

Try a beforeEach() to work with a fresh contract instance for each test.

require Contract = artifacts ...

var contract;
var sender = accounts[0];
var sendee = accounts[1];

beforeEach(function() {
  return Contract.new(sendee) // will default to accounts[0]
  .then(function(instance) {
     contract = instance;
  }
}

...
it("should ... match sendee" ...

If I'm not mistaken, truffle test goes out of its way to avoid acting on .deployed() as a protection so unit tests don't stomp on contracts that are in production.

Hope it helps.

3
  • Hey Rob, thanks for your response. I'm unfortunately still getting an "undefined" output. I pasted the code I used in the "Fourth Attempt" code block in my OP.
    – msafi
    Commented Aug 1, 2018 at 0:11
  • In your fourth attempt, you're trying to read the value synchronously and you're using an odd syntax. You need to contractA._cb().then(function(response) { // now eval response ... Commented Aug 1, 2018 at 16:32
  • Unfortunately, if I try to use _cb() I get an error message of contractA._cB() is not a function, and doing it without the parentheses results in a message of Cannot read property 'then' of undefined
    – msafi
    Commented Aug 1, 2018 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.