0

I have been trying to add to write my own basic tests for a chainlink oracle response. I am adding these tests to Ampleforth's test folder. However, the tests I am running are for the moment very basic.

I am trying to deploy a contract using hardhat - which currently works - and then test a basic function to see if it outputs a number.

This is my test:

import { ethers, upgrades, waffle } from 'hardhat'
import { Contract, Signer, BigNumber, BigNumberish, Event } from 'ethers'
import { TransactionResponse } from '@ethersproject/providers'
import { expect } from 'chai'
import { Result } from 'ethers/lib/utils'
import { imul, increaseTime } from '../utils/utils'

let oracleAPI: Contract
// let oracleAPI: Contract
let mockMarketOracle: Contract
let prevEpoch: BigNumber, prevTime: BigNumber
let deployer: Signer, user: Signer, orchestrator: Signer

console.log("checking test is ran");


async function setupOracle() { 
    console.log("checking setup is ran");
    const [deployer, user, orchestrator] = await ethers.getSigners()
    console.log("got ethers ", deployer);
    oracleAPI = await (await ethers.getContractFactory('APIConsumer'))
        .connect(deployer)
        .deploy()
    await oracleAPI.deployed();

    // return mockMarketOracle;
} 
 
describe('my API tests', function () { 
    before('setting up my API oracle', async () => { 
        await waffle.loadFixture(setupOracle);
        console.log("tx13 ");
    })

    it('not sure what this should do', async function () {
        console.log("tx12 ", oracleAPI);
        // console.log("tx14");
        // console.log( await oracleAPI.testPrint());
        expect( (await oracleAPI.testPrint()) ).to.eq(100);

        const testNumb = await oracleAPI.setPrint(20); 

        await oracleAPI.wait();

        expect( await testNumb ).to.equal(20);
        // console.log("did this print?");
        
    });
})

The only functions I am testing are:

function testPrint() public returns (uint256) {
    return incomeIndex;
}

function setPrint(uint256 t) public returns (uint256) {
    test = t;
    return test;
}

At the tx12 print statement it outputs a contract json with all of the functions, so I am not sure what is going on. I get this error:

1) my API tests
   not sure what this should do:
 AssertionError: expected { Object (hash, type, ...) } to equal 100
  at Context.<anonymous> (test\unit\OracleApiTest.ts:39:52)
  at step (test\unit\OracleApiTest.ts:33:23)
  at Object.next (test\unit\OracleApiTest.ts:14:53)
  at fulfilled (test\unit\OracleApiTest.ts:5:58)
  at processTicksAndRejections (node:internal/process/task_queues:96:5)
  at runNextTicks (node:internal/process/task_queues:65:3)
  at listOnTimeout (node:internal/timers:526:9)
  at processTimers (node:internal/timers:500:7)

Which suggests it's still just looking at the raw contract json object and not waiting for the function call to resolve. Can anyone help?

1 Answer 1

0
  1. You should either mark testPrint() as a view function
function testPrint() public view returns (uint256) {
    return incomeIndex;
}

and then call

expect( (await oracleAPI.testPrint()) ).to.eq(100);

OR

  1. Leave the testPrint() function as it is now and then in your tests call
expect( (await oracleAPI.callStatic.testPrint()) ).to.eq(100);

More info about the callStatic is available here

2
  • That is amazing it works! Thank you so much. Which solution would you say is better practice?
    – Btadford
    Feb 17, 2022 at 20:07
  • The first one, with the view addition
    – Andrej
    Feb 17, 2022 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.