0

Using Truffle and testing against both Truffle's own blockchain and against testrpc yields different results.

I am trying to test that my deployed contract is setting its owner correctly (owner being set to msg.sender in the constructor)

contract Cocktail {
  address public owner;

  function Cocktail() public {
    owner = msg.sender;
  }
}

My Migration script is like

module.exports = (deployer, network, accounts) => {
  const superuser = accounts[0]
  console.log('Deploying to network', network, 'from', superuser)

  deployer.then(() => Cocktail.new({ from: superuser })).then((cocktail) => {
    console.log('Deployed Cocktail with address', cocktail.address)
  })
}

My test is like

const Cocktail = artifacts.require('./Cocktail.sol')

contract('Cocktail', (accounts) => {
  const superuser = accounts[0]

  it('is owned by superuser', () => Cocktail.deployed()
    .then(instance => instance.owner())
    .then((owner) => {
      assert.equal(owner, superuser, `Expected the owner to be '${superuser}'`)
    }))
})

If I just use Truffle's own blockchain and run truffle develop then compile, migrate, then test it's all good.

Contract: Cocktail
  ✓ is owned by superuser

If I fire up testrpc and run truffle test however it claims to migrate okay but the test fails:

Using network 'development'.

Deploying to network development from 0x9aa5b3cee03060172ec9968a49857c2a68341f67
Deployed Cocktail with address 0x2294c2ef0db6994cf0c5a777e10e5be5e7aab69b

  Contract: Cocktail
    1) is owned by superuser
    > No events were emitted

  0 passing (86ms)
  1 failing

  1) Contract: Cocktail is owned by superuser:
     Error: Cocktail has not been deployed to detected network (network/artifact mismatch)

What is going on?

1

This deploy script worked for both testrpc and internal blockchain

const Cocktail = artifacts.require("./Cocktail.sol");

module.exports = (deployer, network, accounts) => {
  const superuser = accounts[0];
  console.log('Deploying to network', network, 'from', superuser);

  deployer.deploy(Cocktail, { from: superuser }).then(() => {
    console.log('Deployed Cocktail with address', Cocktail.address);
  });
};
  • Thanks, that worked. Slowly but surely I'm getting the hang of this stuff :-) – Dave Sag Nov 4 '17 at 3:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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