-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lottery.test.js
105 lines (96 loc) · 3.31 KB
/
Lottery.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const {interface, bytecode } = require('../compile');
let lottery;
let accounts;
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'});
});
describe('Lottery contract',()=>{
it('Deploys a contracts ',()=>{
assert.ok(lottery.options.address);
});
it('allows 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('allows 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('Requires minimum amount of ether to enter ',async()=>{
try{
await lottery.methods.enter().send(
{
from:accounts[0],
value: 0
}
);
//if execution comes here it means it does not throw error.
// So assert(false) will fail the test.
assert(false);
}catch(err)
{
// here if execution comes it means that error occured and test is passed.
assert(err);
}
});
it('Only manager can pick winner ',async()=>{
try{
await lottery.methods.pickWinner().send(
{
from:accounts[1],
value: 0
}
);
//if execution comes here it means it does not throw error.
// So assert(false) will fail the test.
assert(false);
}catch(err)
{
// here if execution comes it means that error occured and test is passed.
assert(err);
}
});
it('sends money to the winner and resets the players array',async()=>{
await lottery.methods.enter().send({
from:accounts[0],
value:web3.utils.toWei('2','ether')
});
const initialBalance = await web3.eth.getBalance(accounts[0]);
await lottery.methods.pickWinner().send({
from:accounts[0]
});
const finalBalance = await web3.eth.getBalance(accounts[0]);
const difference = finalBalance-initialBalance;
assert(difference > web3.utils.toWei('1.8'));
});
});