Solidity

測試抱怨“等待是保留字”

  • December 5, 2017

我為我的契約寫測試。由於某種原因,編譯器抱怨await它是保留字,但它使用得當——其餘的測試函式執行良好。

await is a reserved word (42:2)
 40 |  it (`client buy good on the market - one good available on the market - no goods on the market`, function() {
 41 |      let goodSample = "Fruit";
> 42 |      await market.sell(accounts[0], web3.fromAscii(goodSample), 10, {from: accounts[0], gas: 300000} );
    |      ^
 43 |      let good = await market.getCostForGood(web3.fromAscii(goodSample));
 44 |  
 45 |      let goodResult = await market.buy(web3.fromAscii(goodSample));
   at Parser.pp$5.raise (/home/user/Workspace/myether/datadir1/ttruffle/node_modules/babylon/lib/index.js:4454:13)
   at Parser.pp$3.checkReservedWord (/home/user/Workspace/myether/datadir1/ttruffle/node_modules/babylon/lib/index.js:4347:10)
   at Parser.pp$3.parseIdentifier (/home/user/Workspace/myether/datadir1/ttruffle/node_modules/babylon/lib/index.js:4324:10)
   at Parser.pp$3.parseExprAtom (/home/user/Workspace/myether/datadir1/ttruffle/node_modules/babylon/lib/index.js:3648:21)
   at Parser.pp$3.parseExprSubscripts (/home/user/Workspace/myether/datadir1/ttruffle/node_modules/babylon/lib/index.js:3494:19)
   at Parser.pp$3.parseMaybeUnary (/home/user/Workspace/myether/datadir1/ttruffle/node_modules/babylon/lib/index.js:3474:19)
   at Parser.pp$3.parseExprOps (/home/user/Workspace/myether/datadir1/ttruffle/node_modules/babylon/lib/index.js:3404:19)
   at Parser.pp$3.parseMaybeConditional (/home/user/Workspace/myether/datadir1/ttruffle/node_modules/babylon/lib/index.js:3381:19)
   at Parser.pp$3.parseMaybeAssign (/home/user/Workspace/myether/datadir1/ttruffle/node_modules/babylon/lib/index.js:3344:19)
   at Parser.pp$3.parseExpression (/home/user/Workspace/myether/datadir1/ttruffle/node_modules/babylon/lib/index.js:3306:19)

let market;

beforeEach(`create subject instance before each test`, async function() {
   market = await Market.new();
})

it(`client put good on the market - one good - one good is available on the market`, async function() {
   let goodSample = "Apple";
   await market.sell(accounts[0], web3.fromAscii(goodSample), 10, {from: accounts[2], gas: 300000} );

   let good = await market.getCostForGood(web3.fromAscii(goodSample));

   assert.equal(good[1], 10);
   assert.equal(web3.toUtf8(good[0]), goodSample);
})

it(`client put good on the market - two different goods - two goods are available on the market`, async function() {
   let goodFirstSample = "Fruit";
   let goodSecondSample = "Laptop";
   await market.sell(accounts[0], web3.fromAscii(goodFirstSample), 10, {from: accounts[1], gas: 300000} );
   await market.sell(accounts[0], web3.fromAscii(goodSecondSample), 100, {from: accounts[1], gas: 300000} );

   let goodFirst = await market.getCostForGood(web3.fromAscii(goodFirstSample));
   let goodSecond = await market.getCostForGood(web3.fromAscii(goodSecondSample));

   assert.equal(goodFirst[1], 10);
   assert.equal(web3.toUtf8(goodFirst[0]), goodFirstSample);
   assert.equal(goodSecond[1], 100);
   assert.equal(web3.toUtf8(goodSecond[0]), goodSecondSample);
})

it(`client put good on the market - two same goods - one good with two quantity is available on the market`, function() {

})

it (`client buy good on the market - one good available on the market - no goods on the market`, function() {
   let goodSample = "Fruit";
   await market.sell(accounts[0], web3.fromAscii(goodSample), 10, {from: accounts[0], gas: 300000} );
   let good = await market.getCostForGood(web3.fromAscii(goodSample));

   let goodResult = await market.buy(web3.fromAscii(goodSample));

   assert.equal(good[0], accounts[0]);
   assert.equal(good[1], goodSample);
   assert.equal(good[2], 10);
}) 

任何線索為什麼會發生?

您的最後兩個it語句缺少async關鍵字。

it(`client put good on the market - two same goods - one good with two quantity is available on the market`, function() { 
it (`client buy good on the market - one good available on the market - no goods on the market`, function() {

應該:

it(`client put good on the market - two same goods - one good with two quantity is available on the market`, async function() { 
it (`client buy good on the market - one good available on the market - no goods on the market`, async function() {

此外,我使用 ESlint autofix 清理了程式碼

beforeEach(`create subject instance before each test`, async () => {
       market = await Market.new();
   });

   it(`client put good on the market - one good - one good is available on the market`, async () => {
       const goodSample = 'Apple';
       await market.sell(accounts[0], web3.fromAscii(goodSample), 10, {from: accounts[2], gas: 300000});

       const good = await market.getCostForGood(web3.fromAscii(goodSample));

       assert.equal(good[1], 10);
       assert.equal(web3.toUtf8(good[0]), goodSample);
   });

   it(`client put good on the market - two different goods - two goods are available on the market`, async () => {
       const goodFirstSample = 'Fruit';
       const goodSecondSample = 'Laptop';
       await market.sell(accounts[0], web3.fromAscii(goodFirstSample), 10, {from: accounts[1], gas: 300000});
       await market.sell(accounts[0], web3.fromAscii(goodSecondSample), 100, {from: accounts[1], gas: 300000});

       const goodFirst = await market.getCostForGood(web3.fromAscii(goodFirstSample));
       const goodSecond = await market.getCostForGood(web3.fromAscii(goodSecondSample));

       assert.equal(goodFirst[1], 10);
       assert.equal(web3.toUtf8(goodFirst[0]), goodFirstSample);
       assert.equal(goodSecond[1], 100);
       assert.equal(web3.toUtf8(goodSecond[0]), goodSecondSample);
   });

   it(`client put good on the market - two same goods - one good with two quantity is available on the market`, async () => {

   });

   it(`client buy good on the market - one good available on the market - no goods on the market`, async () => {
       const goodSample = 'Fruit';
       await market.sell(accounts[0], web3.fromAscii(goodSample), 10, {from: accounts[0], gas: 300000});
       const good = await market.getCostForGood(web3.fromAscii(goodSample));

       const goodResult = await market.buy(web3.fromAscii(goodSample));

       assert.equal(good[0], accounts[0]);
       assert.equal(good[1], goodSample);
       assert.equal(good[2], 10);
   });

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