ganache-cli
mocha
const path = require('path');
const assert = require('assert');
describe('测试课程的智能', () => {
it('测试1+2是否等于3', () => {
assert.equal(1 + 2, 3);
})
})
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)
})
})
"test:w": "mocha --watch"
npm run test:w
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。