Solidity

使用 JS 進行 ERC-721 測試 - ReferenceError:未定義使用

  • August 25, 2021

我正在關注本教程:https ://youtu.be/YPbgjPPC1d0 在 34:47 卡住了。這是我寫的程式碼:

const Color = artifacts.require('./Color.sol')

require('chai')
use(require('chai-as-promised'))
should()

contract('Color', (accounts) => {
let contract

describe('deployment', async () => {
   it('deploys successfully', async () => {
contract = await Color.deployed()
const address = contract.address
console.log(address)
assert.notEqual(address, '')
   })
})
})

一切似乎都與影片相匹配,但在使用truffle test 使用網路“開發”之後。

Compiling .\src\contracts\ERC721Full.sol...
ReferenceError: use is not defined
   at Object.<anonymous> (C:/Users/Public/Dapp University/nft/test/Color.test.js:4:1)
   at Module._compile (internal/modules/cjs/loader.js:1072:14)
   at loader (C:\Users\Public\Dapp University\nft\node_modules\babel-register\lib\node.js:144:5)
   at Object.require.extensions.<computed> [as .js] (C:\Users\Public\Dapp University\nft\node_modules\babel-register\lib\node.js:154:7)
   at Module.load (internal/modules/cjs/loader.js:937:32)
   at Function.Module._load (internal/modules/cjs/loader.js:778:12)
   at Module.require (internal/modules/cjs/loader.js:961:19)
   at require (internal/modules/cjs/helpers.js:92:18)
   at C:\Users\BeAWhale\AppData\Roaming\npm\node_modules\truffle\node_modules\mocha\lib\mocha.js:231:27
   at Array.forEach (<anonymous>)
   at Mocha.loadFiles (C:\Users\BeAWhale\AppData\Roaming\npm\node_modules\truffle\node_modules\mocha\lib\mocha.js:228:14)
   at Mocha.run (C:\Users\BeAWhale\AppData\Roaming\npm\node_modules\truffle\node_modules\mocha\lib\mocha.js:536:10)
   at C:\Users\BeAWhale\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\truffle-core\lib\test.js:118:1
   at processTicksAndRejections (internal/process/task_queues.js:95:5)
Truffle v5.0.5 (core: 5.0.5)
Node v14.17.5

有沒有人遇到過類似的問題或知道如何解決?謝謝!

問題在於以下幾行:

require('chai')
use(require('chai-as-promised'))
should()

這些應該是鍊式呼叫,因此應該重寫為:

require('chai')
 .use(require('chai-as-promised'))
 .should()

引用自:https://ethereum.stackexchange.com/questions/107683