简体   繁体   English

在 test.js 文件中使用智能合约变量时出错

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

Trying to use a variable called postCount from my smart contract in my test file which is returning an error undefined尝试在我的测试文件中使用我的智能合约中名为 postCount 的变量,该变量返回未定义的错误

Smart contract looks like:智能合约看起来像:

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

Test file looks like:测试文件如下所示:

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

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

On testing, error being returned is:在测试时,返回的错误是:

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)

Your Contract code should work fine.您的合同代码应该可以正常工作。 The Problem is your js variable postCount is not defined.问题是您的 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