Created
June 6, 2018 17:58
-
-
Save mattlockyer/ba2c654611afee9bd73d9e01ce23d4f5 to your computer and use it in GitHub Desktop.
Tests for HelloMarket smart contract example for Solidity tutorials
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//jshint ignore: start | |
// contracts | |
const contracts = [ | |
{ name: 'HelloMarket' } | |
]; | |
contracts.forEach(c => c.artifact = artifacts.require('./' + c.name + '.sol')) | |
/************************************** | |
* Tests | |
**************************************/ | |
contract('HelloMarket', function(accounts) { | |
let helloMarket, | |
alice = accounts[0], | |
bob = accounts[1]; | |
const aliceMessage = 'Hello World!'; | |
const bobMessage = 'Goodbye World!' | |
it('should be deployed, HelloMarket', async () => { | |
helloMarket = await contracts[0].artifact.deployed(); | |
assert(helloMarket.address !== undefined, contracts[0].name + ' was not deployed'); | |
}); | |
it('should let Alice set the message', async () => { | |
const tx = await helloMarket.talk(aliceMessage, { | |
from: alice | |
}); | |
const message = await helloMarket.message.call(); | |
assert(message === aliceMessage, 'Alice could not set the message'); | |
}); | |
it('should NOT let Bob set the message', async () => { | |
try { | |
const tx = await helloMarket.talk(bobMessage, { from: bob }); | |
assert(false, 'Bob set the message, should NOT have'); | |
} catch (e) { | |
const message = await helloMarket.message.call(); | |
assert(message === aliceMessage, 'Bob set the message, should NOT have'); | |
} | |
}); | |
it('should let Bob buy ownership rights by sending value greater than 0', async () => { | |
const tx = await helloMarket.buyRights({ | |
from: bob, | |
value: 1000 | |
}); | |
const owner = await helloMarket.owner.call(); | |
assert(owner === bob, 'Bob could not buy rights'); | |
}); | |
it('should let Bob set the message', async () => { | |
const tx = await helloMarket.talk(bobMessage, { from: bob }); | |
const message = await helloMarket.message.call(); | |
assert(message === 'Goodbye World!', 'Bob could not set the message'); | |
}); | |
it('should NOT let Alice set the message', async () => { | |
try { | |
const tx = await helloMarket.talk(aliceMessage, { from: alice }); | |
assert(true, 'Alice set the message, should NOT have'); | |
} catch (e) { | |
const message = await helloMarket.message.call(); | |
assert(message === bobMessage, 'Alice set the message, should NOT have'); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment