0
  it("should throw an exception if you try to buy an article that does not exist", function() {
    return ChainList.deployed().then(function(instance) {
        chainListInstance = instance;
        return chainListInstance.sellArticle(articleName, articleDescription, web3.toWei(articlePrice, "ether"), {
          from: seller
        });
      }).then(function(receipt) {
        return chainListInstance.buyArticle(2, {
          from: buyer,
          value: web3.toWei(articlePrice, "ether")
        });
      }).then(assert.fail)
      .catch(function(error) {
        assert(error.message.indexOf('invalid opcode') >= 0, "error message must contain invalid opcode");
      }).then(function() {
        return chainListInstance.articles(articleId);
      }).then(function(data) {
        assert.equal(data[0].toNumber(), articleId, "article id must be " + articleId);
        assert.equal(data[1], seller, "seller must be " + seller);
        assert.equal(data[2], 0x0, "buyer must be empty");
        assert.equal(data[3], articleName, "article name must be " + articleName);
        assert.equal(data[4], articleDescription, "article description must be " + articleDescription);
        assert.equal(data[5].toNumber(), web3.toWei(articlePrice, "ether"), "article price must be " + web3.toWei(articlePrice, "ether"));
      });
  });

1) These is the one of the test file i have seen for my code for SellArticle function i have worked

2) According to my knowledge promise chain excutes by the output given by the first promise to the next

3) And it wait for the promise to complete until then it stays at that point

4) .then(assert.fail) how can it move to the next step if there is no error and can promise chain skip

plese correct me if i am wrong

0

Testing expected exceptions is tricky but also really important so you can confirm the contract rejects all inappropriate requests.

You might find this utility more useful. Xavier has resolved some subtle differences in behavior between geth and ganache-cli (testRPC) in a module you can import. It facilitates a much more readable test.

https://gist.github.com/xavierlepretre/d5583222fde52ddfbc58b7cfa0d2d0a9

Yours would be something like:

it("should throw an exception if you try to buy an article that does not exist", function() {

  return expectedExceptionPromise(function () {
    return chainListInstance.sellArticle(
      articleName, 
      articleDescription, 
      web3.toWei(articlePrice, "ether"), 
        { from: accounts[0], value: 1, gas: 3000000 });
        }, 3000000);

    });
  });
});

The test will complain unless the return value is an exception.

You might notice I carved out the new contract step. A beforeEach can help get each test off to a good start. For example, setting up chainListInstance.

Hope it helps.

0

I usually find using async/await instead of promise chains much more readable and easier to understand. For asserting a transaction failure, you can also use my truffle-assertions library, which has a function specifically for this.

It can be installed with npm:

npm install truffle-assertions

And then imported at the top of your file:

const truffleAssert = require('truffle-assertions');

Using both async/await and truffle-assertions, you could replace your code with the following, which is much more concise.

it("should throw an exception if you try to buy an article that does not exist", async () => {
  let chainListInstance = await ChainList.deployed();
  let receipt = await chainListInstance.sellArticle(articleName, articleDescription, web3.toWei(articlePrice, "ether"), { from: seller });

  truffleAssert.fails(
    chainListInstance.buyArticle(2, { from: buyer, value: web3.toWei(articlePrice, "ether") }),
    truffleAssert.ErrorType.INVALID_OPCODE
  );

  let data = await chainListInstance.articles(articleId);

  assert.equal(data[0].toNumber(), articleId, "article id must be " + articleId);
  assert.equal(data[1], seller, "seller must be " + seller);
  assert.equal(data[2], 0x0, "buyer must be empty");
  assert.equal(data[3], articleName, "article name must be " + articleName);
  assert.equal(data[4], articleDescription, "article description must be " + articleDescription);
  assert.equal(data[5].toNumber(), web3.toWei(articlePrice, "ether"), "article price must be " + web3.toWei(articlePrice, "ether"));
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.