-
Notifications
You must be signed in to change notification settings - Fork 0
/
lottery.test.js
89 lines (73 loc) · 2.42 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
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('deploy',()=>{
assert.ok(lottery.options.address);
});
it('entering multiple',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('no enter', async()=>{
try{
await lottery.methods.enter().send({
from : accounts[3],
value: 0
});
assert(false);
}catch (err) {
assert(err);
}
});
it('manager picking',async ()=>{
try{
await lottery.methods.pickwinner().send({
from : accounts[3]
})
assert(false)
}catch(err){
assert(err)
}
})
it('whole code', 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','ether'));
})
});