Использование Truffle и тестирование как собственного блокчейна Truffle, так и testrpc дает разные результаты.
Я пытаюсь проверить, owner
правильно ли настроен мой развернутый контракт (владелец установлен msg.sender
в конструкторе)
contract Cocktail {
address public owner;
function Cocktail() public {
owner = msg.sender;
}
}
Мой сценарий миграции похож на
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)
})
}
Мой тест похож на
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}'`)
}))
})
Если я просто воспользуюсь собственной цепочкой блоков Truffle и запущу truffle develop
, compile
, migrate
тогда test
все будет хорошо.
Contract: Cocktail
✓ is owned by superuser
Если я запущу testrpc
и запущу truffle test
, он утверждает, что мигрирует нормально, но тест не проходит:
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)
Что происходит?
Этот скрипт развертывания работал как для testrpc, так и для внутреннего блокчейна.
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);
});
};
Дэйв Сэг