Skip to main content
added 413 characters in body
Source Link
goodvibration
  • 26.2k
  • 5
  • 49
  • 89

ThisAs I've mentioned in the comments, there are a number of problems in your code:

  • Calling parseInt(res) may lead to an incorrect result, since res is a BigNumber possibly larger than Number.MAX_SAFE_INTEGER
  • Using 1000000000000000000 may lead to an incorrect result, since it is larger than Number.MAX_SAFE_INTEGER

I'm not sure what exactly the cause of your failure, since I always use async/await instead of then where possible.

But in any case, this seems to be working fine:

contract("Sale", function(accounts) {
    let sale;
    let fromAddr = accounts[0];
    let amountToSend = "1000000000000000000";

    before(async function() {
        sale = await artifacts.require("Sale").new();
    });

    it("Test if can be payed", async function() {
        let res = await web3.eth.sendTransaction({
            from: fromAddr, 
            to: sale.address,  
            value: amountToSend
        });
        // if we reach here then the transaction has completed successfully
    });

    it("Test if contract received ether", async function() {
        let res = await web3.eth.getBalance(sale.address);
        assert(res.greaterThanOrEqualTo(amountToSend));
    });

    it("Catch if balanceOf "+fromAddr, async function() {
        let res = await sale.balanceOf.call(fromAddr);
        assert(res.greaterThanOrEqualTo(1));
    });
});

This seems to be working fine:

contract("Sale", function(accounts) {
    let sale;
    let fromAddr = accounts[0];
    let amountToSend = "1000000000000000000";

    before(async function() {
        sale = await artifacts.require("Sale").new();
    });

    it("Test if can be payed", async function() {
        let res = await web3.eth.sendTransaction({
            from: fromAddr, 
            to: sale.address,  
            value: amountToSend
        });
        // if we reach here then the transaction has completed successfully
    });

    it("Test if contract received ether", async function() {
        let res = await web3.eth.getBalance(sale.address);
        assert(res.greaterThanOrEqualTo(amountToSend));
    });

    it("Catch if balanceOf "+fromAddr, async function() {
        let res = await sale.balanceOf.call(fromAddr);
        assert(res.greaterThanOrEqualTo(1));
    });
});

As I've mentioned in the comments, there are a number of problems in your code:

  • Calling parseInt(res) may lead to an incorrect result, since res is a BigNumber possibly larger than Number.MAX_SAFE_INTEGER
  • Using 1000000000000000000 may lead to an incorrect result, since it is larger than Number.MAX_SAFE_INTEGER

I'm not sure what exactly the cause of your failure, since I always use async/await instead of then where possible.

But in any case, this seems to be working fine:

contract("Sale", function(accounts) {
    let sale;
    let fromAddr = accounts[0];
    let amountToSend = "1000000000000000000";

    before(async function() {
        sale = await artifacts.require("Sale").new();
    });

    it("Test if can be payed", async function() {
        let res = await web3.eth.sendTransaction({
            from: fromAddr, 
            to: sale.address,  
            value: amountToSend
        });
        // if we reach here then the transaction has completed successfully
    });

    it("Test if contract received ether", async function() {
        let res = await web3.eth.getBalance(sale.address);
        assert(res.greaterThanOrEqualTo(amountToSend));
    });

    it("Catch if balanceOf "+fromAddr, async function() {
        let res = await sale.balanceOf.call(fromAddr);
        assert(res.greaterThanOrEqualTo(1));
    });
});
Source Link
goodvibration
  • 26.2k
  • 5
  • 49
  • 89

This seems to be working fine:

contract("Sale", function(accounts) {
    let sale;
    let fromAddr = accounts[0];
    let amountToSend = "1000000000000000000";

    before(async function() {
        sale = await artifacts.require("Sale").new();
    });

    it("Test if can be payed", async function() {
        let res = await web3.eth.sendTransaction({
            from: fromAddr, 
            to: sale.address,  
            value: amountToSend
        });
        // if we reach here then the transaction has completed successfully
    });

    it("Test if contract received ether", async function() {
        let res = await web3.eth.getBalance(sale.address);
        assert(res.greaterThanOrEqualTo(amountToSend));
    });

    it("Catch if balanceOf "+fromAddr, async function() {
        let res = await sale.balanceOf.call(fromAddr);
        assert(res.greaterThanOrEqualTo(1));
    });
});