const path = require('path');
const assert = require('assert');
const Web3 = require('web3')
const ganache = require('ganache-cli')
//const BigNumber = require('bignumber.js')
const web3 = new Web3(ganache.provider())
// 引入合约的json
const CourseList = require(path.resolve(__dirname, '../src/compiled/CourseList.json'))
const Course = require(path.resolve(__dirname, '../src/compiled/Course.json'))
// 定义几个全局变量,所有测试都需要
let accounts
// 实例
let courseList
let course
describe('测试课程的智能', () => {
before(async () => {
// 测试前的数据初始化
accounts = await web3.eth.getAccounts()
console.log(accounts)
// 1. 虚拟部署一个合约
courseList = await new web3.eth.Contract(JSON.parse(CourseList.interface))
.deploy({ data: CourseList.bytecode })
.send({
// 最后一个是创建者——重点
from: accounts[9],
gas: '5000000'
})
})
it('合约部署成功', () => {
assert.ok(courseList.options.address)
})
it('测试添加课程', async () => {
const oldaddress = await courseList.methods.getCourse().call()
assert.equal(oldaddress.length, 0)
await courseList.methods.createCourse(
'蜗牛的React课程'
)
.send({
from: accounts[0],
gas: '5000000'
})
const address = await courseList.methods.getCourse().call()
assert.equal(address.length, 1)
console.log(address)
})
it("添加课程的属性", async () => {
const [address] = await courseList.methods.getCourse().call()
// 添加的课程合约的地址
course = await new web3.eth.Contract(JSON.parse(Course.interface), address)
const name = await course.methods.name().call()
assert.equal(name, '蜗牛的React课程')
})
//删除功能
it("只能ceo能删", async () => {
await courseList.methods.createCourse(
'蜗牛的Vue课程'
)
.send({
from: accounts[0],
gas: '5000000'
})
const address = await courseList.methods.getCourse().call()
assert.equal(address.length, 2)
await courseList.methods.removeCourse(0).send({
from:accounts[9],
gas:'5000000'
})
const address1 = await courseList.methods.getCourse().call()
console.log(address1)
assert.equal(address1.length,1)
})
})
solidity提供的delete()方法删除了地址账号后,不是清除数据,而是将数据清0.
所以得修改合约里的方法
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。