OpenZeppelin / openzeppelin-test-helpers

Assertion library for Ethereum smart contract testing
https://docs.openzeppelin.com/test-helpers
MIT License
415 stars 132 forks source link

the truffle testing process crashes during testing: #177

Closed Waji-nk closed 2 years ago

Waji-nk commented 2 years ago

I'm trying to test a function but truffle keeps throwing me error of AssertionError: Here is my code for the contract.sol: `pragma solidity ^0.8.0; pragma experimental ABIEncoderV2;

contract Wallet { address[] public approvers; uint public quorum; //quorum are the accounts needed to approve

struct Transfer {
    uint id;
    uint amount;
    address payable to;
    uint approval;
    bool sent;
}
//Container for the struct
Transfer[] public transfers;

//Who approved what, mappping is defined for that
mapping(address => mapping(uint => bool)) public approvals; 

constructor(address[] memory _approvers, uint _quorum) public {
    approvers = _approvers;
    quorum = _quorum;
}

function getApprover() external view returns(address[] memory){
    return approvers;
}

//Getter for transfer
function getTransfer() external view returns(Transfer[] memory){
    return transfers;
}

function createTransfer(uint amount, address payable to) external OnlyApprovers(){
transfers.push(Transfer(
  transfers.length,
  amount,
  to,
  0,
  false
 )
 );
}

function approvaTransfer(uint id) external OnlyApprovers(){
    require(transfers[id].sent = false, "Already transfered");
    require(approvals[msg.sender][id] == false , "Cannot transfer twice");

    approvals[msg.sender][id] == true;
    transfers[id].approval++;

    if(transfers[id].approval >= quorum){
        transfers[id].sent = true;
        address payable to = transfers[id].to;
        uint amount = transfers[id].amount;
        to.transfer(amount);

    }
}

receive() external payable {

}

modifier OnlyApprovers() {
    bool allowed = false;
    for(uint i=0; i < approvers.length; i++){
        if(approvers[i] == msg.sender){
            allowed = true;
        }
    }
    require(allowed = true, "You are not allowed to access/ Only approvers allowed");
    _;
}

}`

and here is the test i wrote:

const { expectRevert } = require("@openzeppelin/test-helpers"); 
const Wallet = artifacts.require("Wallet"); //Define the artifact

contract("Wallet", (accounts) => {
  //Define a contract block, all our test will be inside this block
  let wallet; //variable that points to the deployed blockchain
  beforeEach(async () => {
    //This will run before each test, this is where we'll deploy our smart contract
    wallet = await Wallet.new(
      [accounts[0], accounts[1], accounts[2], accounts[3]],
      3
    ); //arguments will be those which we defined in our smart contract(constructor i.e Address[] and quorum)
    web3.eth.sendTransaction({
      from: accounts[0],
      to: wallet.address,
      value: 100000,
    });
  });

  it("should have correct approvers and quourums", async () => {
    const approvers = await wallet.getApprover();
    const quorum = await wallet.quorum();
    assert(approvers.length === 4); //assertions are test we do on data and if one is failed whole is gonna fail
    assert(approvers[0] === accounts[0]);
    assert(approvers[1] === accounts[1]);
    assert(approvers[2] === accounts[2]);
    assert(approvers[3] === accounts[3]);
    assert(quorum.toNumber() === 3);
  });

  //Test Createtransfer Test() (1/2)
  it("Should create transfer", async () => {
    await wallet.createTransfer(100, accounts[4], { from: accounts[0] }); //We dont assign this to any variable like above is because it's a payble function and not a view function
    const transfers = await wallet.getTransfer();
    assert(transfers.length === 1);
    assert(transfers[0].id === "0");
    assert(transfers[0].amount === "100");
    assert(transfers[0].to === accounts[4]);
    assert(transfers[0].approval === "0");
    assert(transfers[0].sent === false);
  });
//Test Createtransfer Test() (2/2)
  it("should NOT create transfers if sender is not approved", async () => {
    await expectRevert(
      wallet.createTransfer(100, accounts[5], { from: accounts[4] }),
      "only approver allowed"
    );
  });
}):

Test Createtransfer Test() (2/2) is throwing error:

Screenshot 2021-12-19 at 2 30 48 PM
Waji-nk commented 2 years ago

require(allowed = true, "You are not allowed to access/ Only approvers allowed");

That is where error was, should be "==" instead of "=", solved it. closing the issue.