I am trying to follow a tutorial where I'm creating a EthSwap app from scratch. I am writing a unit test where I'm trying to test the buyTokens() function.
I'm trying to check if the EthSwap balance has decreased after buying tokens.
This is the test function:
describe('buyTokens()', async () => {
let result
before(async () => {
//purchase tokens before each example
result = await ethSwap.buyTokens({from : investor, value: web3.utils.toWei('1','ether')})
})
it("Allows users to instantly purchase tokens from EthSwap for a fixed price", async () =>{
//check investor token balance after purchase
let investorBalance = await token.balanceOf(investor);
assert.equal(investorBalance.toString(), tokens('100'))
//check EthSwap balance after purchase
let ethSwapBalance
ethSwapBalance = await token.balanceOf(ethSwap.address)
assert.equal(ethSwapBalance.toString(), tokens('999900'))
})
})
After running truffle test it shows this: AssertionError: expected '1000000000000000000000000' to equal '999900000000000000000000'
However the change in balance is reflected in the test results investorBalance, i.e., when I was running the following snippet, the test case was passing.
describe('buyTokens()', async () => {
let result
before(async () => {
//purchase tokens before each example
result = await ethSwap.buyTokens({from : investor, value: web3.utils.toWei('1','ether')})
})
it("Allows users to instantly purchase tokens from EthSwap for a fixed price", async () =>{
//check investor token balance after purchase
let investorBalance = await token.balanceOf(investor);
assert.equal(investorBalance.toString(), tokens('100'))
//check EthSwap balance after purchase
//let ethSwapBalance
//ethSwapBalance = await token.balanceOf(ethSwap.address)
//assert.equal(ethSwapBalance.toString(), tokens('999900'))
})
})
This is my EthSwap.sol
pragma solidity ^0.5.0;
import "./Token.sol";
contract EthSwap{
string public name = "EthSwap Instant Exchange";
Token public token;
uint public rate = 100;
constructor(Token _token) public{
token = _token;
}
function buyTokens()public payable{
//calculation of number of tokens
uint tokenAmount = msg.value * rate;
token.transfer(msg.sender, tokenAmount);
}
}
Token.sol:
pragma solidity ^0.5.0;
contract Token{
string public name = "Ray Token";
string public symbol = "RAY";
uint256 public totalSupply = 1000000000000000000000000;
uint8 public decimals = 18;
event Transfer(
address indexed _from,
address indexed _to,
uint256 _value
);
event Approval(
address indexed _owner,
address indexed _spender,
uint256 _value
);
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
constructor() public{
balanceOf[msg.sender] = totalSupply;
}
function transfer(address _to, uint256 _value) public returns (bool success){
require(balanceOf[msg.sender] >= _value);
balanceOf[msg.sender] >= _value;
balanceOf[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
function approve(address _spender, uint256 _value) public returns (bool success) {
allowance[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender,_value);
return true;
}
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success){
require(_value <= balanceOf[_from]);
require(_value <= allowance[_from][msg.sender]);
balanceOf[_from] -= _value;
balanceOf[_to] -= _value;
allowance[_from][msg.sender] -= _value;
emit Transfer(_from, _to, _value);
return true;
}
}
How is the change reflected in the investorBalance but not in EthSwap balance? The tutorial is using chai.
This is the entire EthSwap.test.js
const Token = artifacts.require('Token')
const EthSwap = artifacts.require('EthSwap')
require('chai')
.use(require('chai-as-promised'))
.should()
function tokens(n){
return web3.utils.toWei(n,'ether')
}
contract('EthSwap',([deployer,investor]) => {
let token,ethSwap
before(async() => {
token = await Token.new()
ethSwap = await EthSwap.new(token.address)
await token.transfer(ethSwap.address,tokens('1000000'))
})
describe('Token deployment',async() =>{
it('contract has a name', async() =>{
const name = await token.name()
assert.equal(name,'Ray Token')
})
})
describe('EthSwap deployment',async() =>{
it('contract has a name', async() =>{
const name = await ethSwap.name()
assert.equal(name,'EthSwap Instant Exchange')
})
it('contract has tokens', async() => {
let balance = await token.balanceOf(ethSwap.address)
assert.equal(balance.toString(), tokens('1000000'))
})
})
describe('buyTokens()', async () => {
let result
before(async () => {
//purchase tokens before each example
result = await ethSwap.buyTokens({from : investor, value: web3.utils.toWei('1','ether')})
})
it("Allows users to instantly purchase tokens from EthSwap for a fixed price", async () =>{
//check investor token balance after purchase
let investorBalance = await token.balanceOf(investor);
assert.equal(investorBalance.toString(), tokens('100'))
//check EthSwap balance after purchase
let ethSwapBalance
ethSwapBalance = await token.balanceOf(ethSwap.address)
assert.equal(ethSwapBalance.toString(), tokens('999900'))
})
})
})
Edit:
it("Allows users to instantly purchase tokens from EthSwap for a fixed price", async () =>{
//check investor token balance after purchase
let investorBalance = await token.balanceOf(investor);
assert.equal(investorBalance.toString(), tokens('100'))
//check EthSwap balance after purchase
let ethSwapBalance
// ethSwapBalance = await token.balanceOf(ethSwap.address)
// assert.equal(ethSwapBalance.toString(), tokens('999900'))
ethSwapBalance = await web3.eth.getBalance(ethSwap.address)
assert.equal(ethSwapBalance.toString(), web3.utils.toWei('1','Ether'))
})
I did the next step of the tutorial, and the test case passes. Why?