-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
170 lines (163 loc) · 6.64 KB
/
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
let Auction = artifacts.require("./Auction.sol");
let auctionInstance;
contract('AuctionContract', function (accounts) {
//accounts[0] is the default account
//Test case 1
it("Contract deployment", function() {
//Fetching the contract instance of our smart contract
return Auction.deployed().then(function (instance) {
//We save the instance in a gDlobal variable and all smart contract functions are called using this
auctionInstance = instance;
assert(auctionInstance !== undefined, 'Auction contract should be defined');
});
});
//Sample Test Case
it("Should set bidders", function() {
return auctionInstance.register({from:accounts[1]}).then(function(result) {
return auctionInstance.getPersonDetails(0);
}).then(function(result) {
assert.equal(result[2], accounts[1], 'bidder address set');
})
});
//Test Case for checking if the bid is more than the token amount
it("Should NOT allow to bid more than remaining tokens", function() {
/**********
TASK 1: Call bid method from accounts[1] of Auction.sol using auctionInstance and
pass itemId=0, count=6 as arguments
HINT: To make a function call from account 1 use {from: accounts[1]} as an extra argument
***********/
return auctionInstance.bid(0,6,{from:accounts[1]})
.then(function (result) {
/*
We are testing for a negative condition and hence this particular block will not have executed if our test case was correct. If this part is executed then we throw an error and catch the error to assert false
*/
throw("Failed to check remaining tokens less than count");
}).catch(function (e) {
var a = e.toString();
if(e === "Failed to check remaining tokens less than count") {
/**********
TASK 2: This is the error which we had thrown. Should you assert true or false?
HINT: Use assert(false) to assert false
Use assert(true) to assert true
***********/
/*<CODE HERE>*/
assert(true);
} else {
/**********
TASK 3: assert the opposite here
***********/
/*<CODE HERE>*/
assert(false);
}
})
});
//Modifier Checking
it("Should NOT allow non owner to reveal winners", function() {
/**********
TASK 4: Call revealWinners from account 1
***********/
return auctionInstance.revealWinners({from:accounts[1]})
.then(function (instance) {
/*
We are testing for a negative condition and hence this particular block will not have executed if our test case was correct. If this part is executed then we throw an error and catch the error to assert false
*/
throw("Failed to check owner in reveal winners");
}).catch(function (e) {
if(e === "Failed to check owner in reveal winners") {
/**********
TASK 5: This is the error which we had thrown. Should you assert true or false?
HINT: Use assert(false) to assert false
Use assert(true) to assert true
***********/
/*<CODE HERE>*/
assert(true);
} else {
/**********
TASK 6: assert the opposite here
***********/
/*<CODE HERE>*/
assert(false);
}
})
})
it("Should set winners", function() {
/**********
TASK 7: Call register function from account 2
***********/
return auctionInstance.register({from:accounts[2]})
.then(function(result) {
/**********
TASK 8: Call register function from account 3
***********/
return auctionInstance.register({from:accounts[3]})
}).then(function() {
/**********
TASK 9: Call register function from account 4
***********/
return auctionInstance.register({from:accounts[4]})
}).then(function() {
/**********
TASK 10: Call bid method from accounts[2] of Auction.sol using auctionInstance and
pass itemId=0, count=5 as arguments
***********/
return auctionInstance.bid(0,5,{from:accounts[2]})
}).then(function() {
/**********
TASK 11: Call bid method from accounts[3] of Auction.sol using auctionInstance and
pass itemId=1, count=5 as arguments
***********/
return auctionInstance.bid(1,5,{from:accounts[3]})
}).then(function() {
/**********
TASK 12: Call bid method from accounts[4] of Auction.sol using auctionInstance and
pass itemId=2, count=5 as arguments
***********/
return auctionInstance.bid(2,5,{from:accounts[4]})
}).then(function() {
/**********
TASK 13: Call revealWinners function from accounts[0]
***********/
return auctionInstance.revealWinners({from:accounts[0]})
}).then(function() {
/**********
TASK 14: call winners function from accounts[0] to get the winner of item id 0
***********/
return auctionInstance.winners(0,{from:accounts[0]})
}).then(function(result) {
/**********
TASK 15: assert to see if the winner address is not the default address
HINT: Default address is '0x0000000000000000000000000000000000000000'
Use notEqual method of assert
Parameters for notEqual : (result, default address , message);
***********/
/*<CODE HERE>*/
assert.notEqual(result,'0x0000000000000000000000000000000000000000','Owner shouldn`t bid');
/**********
TASK 16: call winners function from accounts[0] to get the winner of item id 1
***********/
return auctionInstance.winners(1,{from:accounts[0]})
}).then(function(result) {
/**********
TASK 17: assert to see if the winner address is not the default address
HINT: Default address is '0x0000000000000000000000000000000000000000'
Use notEqual method of assert
Parameters for notEqual : (result, default address , message);
***********/
/*<CODE HERE>*/
assert.notEqual(result,'0x0000000000000000000000000000000000000000','Owner shouldn`t bid');
/**********
TASK 18: Call winners function from account 3 to get the winner of item id 2
***********/
return auctionInstance.winners(2,{from:accounts[0]})
}).then(function(result) {
/**********
TASK 19: assert to see if the winner address is not the default address
HINT: Default address is '0x0000000000000000000000000000000000000000'
Use notEqual method of assert
Parameters for notEqual : (result, default address , message);
***********/
/*<CODE HERE>*/
assert.notEqual(result,'0x0000000000000000000000000000000000000000','Owner shouldn`t bid');
})
});
});