-
Given: pragma solidity ^0.7.0;
contract ReturnValues {
function getValues() public pure returns (uint8[3] memory values, uint8[3] memory dates) {
values = [1, 2, 3];
dates = [4, 5, 6];
return (values, dates);
}
} I would expect this test to pass: const { expect } = require('chai')
describe('ReturnValues', function () {
let contract
beforeEach(async function () {
const ReturnValues = await ethers.getContractFactory('ReturnValues')
contract = await ReturnValues.deploy()
await contract.deployed()
})
it('should return object with keys "values" and "dates"', async function () {
// when
const result = await contract.getValues()
// then
expect(result).to.haveOwnProperty('values')
expect(result).to.haveOwnProperty('dates')
})
}) But it fails because the result object looks like this: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The result is an Array (with extra keys), and Result objects don’t overwrite properties defined on them (including on the prototype; this would break operations on them). So if a name collides with part of the Array prototype, those keys are dropped and must be accessed positionally. If you adjust your signature you pass in though, you can resolve this, for example: It looks like you are using Hardhat, ( Does that help? |
Beta Was this translation helpful? Give feedback.
-
That makes sense, thank you. I had a feeling that's probably a reserved name and now I know why 👍🏻 |
Beta Was this translation helpful? Give feedback.
The result is an Array (with extra keys), and Result objects don’t overwrite properties defined on them (including on the prototype; this would break operations on them). So if a name collides with part of the Array prototype, those keys are dropped and must be accessed positionally.
If you adjust your signature you pass in though, you can resolve this, for example:
ABI = [ "function getValues() pure returns (uint8[3] _values, uint8[3] dates)" ]
.It looks like you are using Hardhat, (
getContractFactory
is something they inject; not part of ethers), so I’m not sure how to override the ABI but you could also re-instantiate the contract object viacontract = new Contract(contract.address, ab…