簡體   English   中英

在 test.js 文件中使用智能合約變量時出錯

[英]Error using a smart contract variable in test.js file

嘗試在我的測試文件中使用我的智能合約中名為 postCount 的變量,該變量返回未定義的錯誤

智能合約看起來像:

contract SocialNetwork {
string public name;
uint public postCount = 0;

測試文件如下所示:

describe ('posts', async ()=> {
        let result

        before(async ()=> {
            result = await socialNetwork.createPost('This is my first post', {
                from: author
            })
            postCount = await socialNetwork.postCount()
        })

在測試時,返回的錯誤是:

1) Contract: SocialNetwork
   posts
     "before all" hook:
 ReferenceError: postCount is not defined
  at _callee5$ (D:/projex/socialmedia-blockchain/test/SocialNetwork.js:33:20)
  at tryCatch (node_modules\regenerator-runtime\runtime.js:65:40)
  at Generator.invoke [as _invoke] (node_modules\regenerator-runtime\runtime.js:303:22)
  at Generator.prototype.(anonymous function) [as next] (node_modules\regenerator-runtime\runtime.js:117:21)
  at step (test\SocialNetwork.js:5:191)
  at D:\projex\socialmedia-blockchain\test\SocialNetwork.js:5:361
  at run (node_modules\core-js\modules\es6.promise.js:75:22)
  at D:\projex\socialmedia-blockchain\node_modules\core-js\modules\es6.promise.js:92:30
  at flush (node_modules\core-js\modules\_microtask.js:18:9)
  at process._tickCallback (internal/process/next_tick.js:172:11)

您的合同代碼應該可以正常工作。 問題是您的 js 變量 postCount 未定義。

describe ('posts', async ()=> {
    let result;
    let postCount;

    before(async ()=> {
        result = await socialNetwork.createPost('This is my first post', {
            from: author
        })
        postCount = await socialNetwork.postCount()
    })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM