Skip to content

Instantly share code, notes, and snippets.

@sai-sondarkar
Created December 9, 2018 08:27
Show Gist options
  • Save sai-sondarkar/3027da84a2daeaf1ce201bc456d259e0 to your computer and use it in GitHub Desktop.
Save sai-sondarkar/3027da84a2daeaf1ce201bc456d259e0 to your computer and use it in GitHub Desktop.
const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const provider = ganache.provider();
const web3 = new Web3(provider);
const {interface,bytecode} = require('../compile');
let accounts;
let lottery;
beforeEach(async ()=>{
accounts = await web3.eth.getAccounts();
lottery = await new web3.eth.Contract(JSON.parse(interface))
.deploy({data: bytecode})
.send({from: accounts[0],gas: '1000000'});
// inbox.setProvider(provider);
//then use any one accont to deploy our contract.
});
describe('Lottery Contract Test',()=>{
it('test',()=>{
assert.ok(lottery.options.address);
});
it('Allow one account to enter', async()=>{
await lottery.methods.enter().send({
from: accounts[0],
value: web3.utils.toWei('0.02','ether')
});
const players = await lottery.methods.getPlayers().call({
from: accounts[0]
});
assert.equal(accounts[0],players[0]);
assert.equal(1,players.length);
});
it('Allow multiple account to enter', async()=>{
await lottery.methods.enter().send({
from: accounts[0],
value: web3.utils.toWei('0.02','ether')
});
await lottery.methods.enter().send({
from: accounts[1],
value: web3.utils.toWei('0.02','ether')
});
await lottery.methods.enter().send({
from: accounts[2],
value: web3.utils.toWei('0.02','ether')
});
const players = await lottery.methods.getPlayers().call({
from: accounts[0]
});
assert.equal(accounts[0],players[0]);
assert.equal(accounts[1],players[1]);
assert.equal(accounts[2],players[2]);
assert.equal(3,players.length);
});
it('require a minimum amount of ether to enter', async()=>{
try{
await lottery.methods.enter().send({
from: accounts[0],
value: 0
});
assert(false)
}catch(err){
assert(err);
}
});
it('only manager can enter', async()=>{
try{
await lottery.methods.pickWinner().send({
from: accounts[1]
});
assert(false);
}catch(err){
assert(err);
}
});
it('sends money to the winner and resets the plpayer array',async()=>{
await lottery.methods.enter().send({
from: accounts[0],
value: web3.utils.toWei('2','ether')
});
const initalBalance = await web3.eth.getBalance(accounts[0]);
await lottery.methods.pickupWinner().send({
from: accounts[0]
})
const finalBalance = await web3.eth.getBalance(accounts[0]);
const difference = finalBalance - initalBalance;
assert(difference>web3.utils.toWei('1.8','ether'));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment