cryptopunks测试代码cryptopunksmarket-setinitial.js

require('babel-polyfill');
//测试用例要在执行完了truffle compile和truffle migrate后才能使用truffle test来进行测试
//要注意artifacts只有在truffle框架里能使用
var CryptoPunksMarket = artifacts.require("./CryptoPunksMarket.sol"); //要注意contract只有在truffle框架里能使用,mocha中使用describe块开始
contract('CryptoPunksMarket-setInitial', function (accounts) {
//it为测试用例,是同步进行访问的,前面的string是用来解释该测试用例的测试目的是什么
it("Should start with 0 balance", async function () {
//声明使用的是truffle migrate部署好后的CryptoPunksMarket智能合约
var contract = await CryptoPunksMarket.deployed(); //然后调用里面的setInitialOwner初始化用户函数,将punk0的拥有者设为accounts[0]
await contract.setInitialOwner(accounts[0], 0);
//然后通过查看得到用户accounts[0]拥有的token数量
var balance = await contract.balanceOf.call(accounts[0]);
//这就是chai的断言部分,查看balance.valueOf()是否与1相等,如果是,则继续向下运
//行,如果不成功将会输出错误信息Didn't get the initial punk,而且该测试用例it回结
//束,最后会标明出错
assert.equal(balance.valueOf(), 1, "Didn't get the initial punk");
//查看punkIndex为0的拥有者是谁
var owner = await contract.punkIndexToAddress.call(0);
//拥有者应该是accounts[0],否则报错
assert.equal(owner, accounts[0], "Ownership array wrong");
//初始化punk有10000个,分配一个给accounts[0]后,应该还剩9999个
var remaining = await contract.punksRemainingToAssign.call();
assert.equal(9999, remaining); // todo Set this back to 10000 for final runs
var assignCoins = 100;
//给用户accounts[0]分配99个punk,包括之前那个,应该有100个
for (var i=1; i<assignCoins; i++) {
await contract.setInitialOwner(accounts[0], i);
}
//所以现在剩余的punk有10000-100个
var remainingAfter = await contract.punksRemainingToAssign.call();
assert.equal(10000-assignCoins, remainingAfter); var balanceAfter = await contract.balanceOf.call(accounts[0]);
assert.equal(assignCoins, balanceAfter); }),
//该用例也是用来分配punk的
it("bulk assign", async function () {
//该声明使用的合约与上面的测试用例是一样的,但是这里我会遇见一个问题,就是根据
//这个函数运行的逻辑来看,该it是在上面的it结束后才运行的,这样才能保证此时被分配
//的punk有110个,我们truffle test运行起来后发现的确是成功的,但是如果我们没有
//truffle的框架中运行的话,it其实是会同步运行的,所以我想可能是truffle在包装的时候
//进行了设置,所以它没有使用describe测试套件,而是使用了自定义的contract,这是我
//还存疑的地方
var contract = await CryptoPunksMarket.deployed();
var owners = [accounts[0], accounts[1], accounts[2], accounts[3], accounts[4], accounts[5], accounts[6], accounts[7], accounts[8], accounts[9]];
var punks = [1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009];
await contract.setInitialOwners(owners, punks);
for (var i = 0; i < 10; i++) {
var currentOwner = await contract.punkIndexToAddress.call(punks[i]);
assert.equal(currentOwner, owners[i]);
}
var remainingAfter = await contract.punksRemainingToAssign.call();
assert.equal(10000-110, remainingAfter);
}),
it("can not pass an invalid index to assign initial", async function () {
var contract = await CryptoPunksMarket.deployed();
try {
await contract.setInitialOwner(accounts[0], 10000);
assert(false, "Should have thrown exception.");
} catch (err) {
// Should catch an exception
} }),
it("only owner can assign initial", async function () {
var contract = await CryptoPunksMarket.deployed();
try {
await contract.setInitialOwner(accounts[1], 1);
assert(false, "Should have thrown exception.");
} catch (err) {
// Should catch an exception
} }),
it("Can not claim punk after set initial owners assigned", async function () {
var contract = await CryptoPunksMarket.deployed();
await contract.allInitialOwnersAssigned();
try {
await contract.setInitialOwner(accounts[0], 0);
assert(false, "Should have thrown exception.");
} catch (err) {
// Should catch an exception
} })
});

上面这个就是测试代码cryptopunksmarket-setinitial.js及其一些解释

上一篇:一、PTA实验作业


下一篇:.Net 上传文件到ftp服务器和下载文件