How to test accepting ERC20 tokens as payment?

Hi!

I am trying to test a function to accept ERC20 as payment, but unfortunately it’s not working, throwing the error: " Error: Returned error: VM Exception while processing transaction: revert".I know this error is not very detailed and there can be many reasons why it happens, but I can’t wrap my head around the logic behind it. When I test the function in the front end, it works perfectly, but trying to test it with ganache, it fails. I can’t seem to find anywhere an example of how it should work, though I know you must call “approve” and “transferFrom” in order to make the ERC20 payment. I’m using ganache-cli with fork on mainnet. Can someone please point me to the right direction?Should I use the mock contract and call approve and transferFrom or should I use the function in the crowdsale contract that already has approve and transferFrom there? This is my code so far:

 it('should accept payment - LINK to USD with price feed', async function(){
    const priceConversion = await this.link.getLinkPrice();
    const ede = priceConversion.div(web3.utils.toBN(100000000));
    const investmentAmount = ether('0.1');
    const linkPriceInUsd = ede.mul(investmentAmount);
    const expectedTokenAmount = linkPriceInUsd.mul(linkRate);
   // await this.linkToken.approve(investor, investmentAmount, {from:creator});
   //await this.linkToken.transferFrom(investor, creator, investmentAmount,{from: investor});
    await this.crowdsale.buyTokensWithLINK(investor, {value: investmentAmount,from:investor});
    expect(await this.token.balanceOf(investor)).to.be.bignumber.equal(expectedTokenAmount);
  })

Using the function in the crowdsale fails with the error above and if I use the ERC20 mock contract it says " Error: Returned error: VM Exception while processing transaction: revert ERC20: transfer amount exceeds balance – Reason given: ERC20: transfer amount exceeds balance."

Hi @EMarie1571 - did you manage to ever fix this error? I'm getting the same. Thank you!

Please can you share me the contract, I need to integrate ERC token as payment, but I don't know how to go about that.