I am currently following my first Ethereum course, and I am trying to test the a very simple contract, but it keeps failing. When I run the contract on Remix, everything seems to be working fine, so I'm assuming it's the test setup. I would appreciate any input.
The contract:
pragma solidity ^0.4.17;
contract Loaner {
address public lender;
address public borrower;
function Lottery() public {}
function setLender() public {
lender = msg.sender;
}
function setBorrower() public {
borrower = msg.sender;
}
}
My test code:
const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const { interface, bytecode } = require('../compile');
let loaner;
let accounts;
before(async () => {
accounts = await web3.eth.getAccounts();
loaner = await new web3.eth.Contract(JSON.parse(interface))
.deploy({ data: bytecode })
.send({ from: accounts[0], gas: '1000000' });
})
describe('Loaner', () => {
it('deploys a contract', () => {
assert.ok(loaner.options.address);
});
it('allows setting a lender', async () => {
await loaner.methods.setLender().call({ from: accounts[0] });
const lender = await loaner.methods.lender().call({ from: accounts[0] });
assert.equal(lender, accounts[0]);
});
it('allows setting a borrower', async () => {
await loaner.methods.setBorrower().call({ from: accounts[0] });
const borrower = await loaner.methods.lender().call({ from: accounts[0] });
assert.equal(borrower, accounts[0]);
});
})
Results:
Loaner
✔ deploys a contract
1) allows setting a lender
2) allows setting a borrower
1 passing (108ms)
2 failing
1) Loaner
allows setting a lender:
AssertionError [ERR_ASSERTION]: '0x0000000000000000000000000000000000000000' == '0xA441ACf8c2f84D3A4cBb53c0c07f935ee7cc271C'
+ expected - actual
-0x0000000000000000000000000000000000000000
+0xA441ACf8c2f84D3A4cBb53c0c07f935ee7cc271C
at Context.<anonymous> (test/Lonaer.test.js:26:16)
at processTicksAndRejections (node:internal/process/task_queues:94:5)
2) Loaner
allows setting a borrower:
AssertionError [ERR_ASSERTION]: '0x0000000000000000000000000000000000000000' == '0xA441ACf8c2f84D3A4cBb53c0c07f935ee7cc271C'
+ expected - actual
-0x0000000000000000000000000000000000000000
+0xA441ACf8c2f84D3A4cBb53c0c07f935ee7cc271C
at Context.<anonymous> (test/Lonaer.test.js:32:16)
at processTicksAndRejections (node:internal/process/task_queues:94:5)