I'm trying to test my escrow contract, where Alice sends ETH to escrow, escrow approves and then ETH goes to Bob. When Alice sends ETH, on-hold balance of Bob is increased (in a mapping).
Everything is working great when deployed and interacting directly in remix with several accounts. However when testing it with test_escrow.sol, on-hold balance of bob isn't increased.
Indeed, at Assert.equal(escr.getBalanceOnHold(bob), msg.value, 'value in Bob mapping should be 100wei');
, escr.getBalanceOnHold(bob) = 0
when it should be 100.
I feel like I'm missing something on scope stuff
Here is the code:
Escrow.sol :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "./console.sol";
contract EscrowContract {
/* escrow_agent has all the right to use the SC, it is the deployer of it*/
address escrow_agent;
modifier onlyEscrow() {
require(msg.sender == escrow_agent);
_;
}
/* mapping address of beneficiary to an amount deposited */
mapping(address => uint256) public deposits;
/* constructor only run once, when the SC is deployed */
constructor () {
escrow_agent = msg.sender;
}
function getEscrow() public view returns(address) {
return escrow_agent;
}
/* beneficiary is the one who will receive the amount */
function deposit(address beneficiary) payable public {
uint256 amount = msg.value;
deposits[beneficiary] = amount;
}
/* withdraw founds to the beneficiary if process is done */
function withraw(address payable beneficiary) public onlyEscrow {
uint256 amount = deposits[beneficiary];
beneficiary.transfer(amount);
}
function getBalanceOnHold(address beneficiary) public view returns(uint) {
return deposits[beneficiary] ;
}
}
Escrow_test.sol :
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0;
import "remix_tests.sol";
import "remix_accounts.sol";
import "../contracts/Escrow.sol";
import "../contracts/console.sol";
/* Alice wants to send ETH to Bob in exchange for a service through escrow_user. */
contract testEscrow {
EscrowContract escr;
/* Set users */
address escrow_user;
address alice = TestsAccounts.getAccount(1);
address bob = TestsAccounts.getAccount(2);
function beforeAll() public {
escr = new EscrowContract();
escrow_user = escr.getEscrow(); // address of the smart contract deployer
}
function testEscrowUser() public {
Assert.equal(escrow_user, escr.getEscrow(), 'escrow should be SC deployer i.e escrow_user');
}
/* Verify that on-hold balance of a user is 0 by default */
function testBalanceDefault() public {
Assert.equal(escr.getBalanceOnHold(msg.sender), 0, 'Balance of user should be 0 by default');
}
/* Check if Alice can send ETH to Bob and if Bob mapping is correct */
/// #sender: account-1
/// #value: 100
function testDeposit() public payable {
Assert.equal(msg.sender, alice, 'caller should be Alice');
escr.deposit(bob);
Assert.equal(msg.value, 100, 'value sent should be 100wei');
Assert.equal(escr.getBalanceOnHold(bob), msg.value, 'value in Bob mapping should be 100wei');
}
/*
/// #sender: account-1
/// #value: 100
function checkSenderAndValue() public payable {
// account index varies 0-9, value is in wei
Assert.equal(msg.sender, TestsAccounts.getAccount(1), "Invalid sender");
Assert.equal(msg.value, 100, "Invalid value");
}
*/
}