在 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()
    })