AssertionError: object tested must be an array, a map, an object, a set, a string, or a weakset, but number given

Hi Team,
Firstly I tried my best and all possibilities but not sure how I can resolve this issue.

So basically I am verifying API response through .csv file.
My.csv file data header and my script looks fine but no idea why below error appear:
image
“AssertionError: object tested must be an array, a map, an object, a set, a string, or a weakset, but number given”

Step to reproduce:

  1. Hit the API
  2. Get the response:
    {
    “earnings”: {
    “commissionLastWeek”: {
    “amount”: 13490064.0000046,
    “currencyCode”: “USD”,
    “changePercentage”: -10.414756619131635
    },
    “bonusesLastMonth”: {
    “amount”: 28347387.119998183,
    “currencyCode”: “USD”,
    “changePercentage”: 4.433203161986422
    },
    “incomeLastMonth”: {
    “amount”: 43152444.72000217,
    “currencyCode”: “USD”,
    “changePercentage”: 4.433203161986422
    }}},
    image
  3. Verify the response:
    pm.test(“data verificationn”, function () {
    let jsonData = pm.response.json();
    pm.expect(jsonData.earnings.commissionLastWeek.amount).to.eql(Number(data.earningscommissionLastWeekamount));
    });

Error: AssertionError: object tested must be an array, a map, an object, a set, a string, or a weakset, but number given

P.S.

  1. I tried to get type of particular attribute through console logs and its say “number” but sill error exist.

  2. I give “include” in script since .csv file not save value as “13490064.0000046”, Its always save as “13490064”.
    image
    We can give “eql or equal” if value is in integer.

So how we can resolve issue with “include”?
How we can save .csv file with full value “13490064.0000046”?

Let me know still needed more details.

I would try to better isolate the source of the error you are getting. It seems to me that the error is originating in the first expectation. (the one with .to.include), not in the second one, as you seem to think.

It looks like projectedPremierVolumeThisMonth is not an array or similar.

Hi @aakashjain8693,

Could you drill down the assertion block one by one and get identify which response data gives the actual problem. If you want to validate the first part of the value as you described in your verification part. Try to store in a variable and split using dot operator then you can assert with your expected value. thanks

@vdespa No I was following the second linde of code only not the first one.
You can see the reference here.

You can see the console, required path have exact the same number.

Looks like my script too perfect cause when I add equal only, Verification failed as below with incorrect number. It mean path is same.


@prabhumohan09 Hope clear with some sets of cases.

@vdespa @allen.helton Any suggestion?
How I can verify in other ways?
How I can write value with decimal operator permanently in .csv file without format always?

@vdespa @prabhumohan09 I tried with Match assertion and working fine with hard code expected value:

But when trying to verify data through .csv file, error occured. Tried with “”, (), {} but still no luck.


AssertionError: expected 13490064.0000046 to match /data.earningscommissionLastWeekamount/

You could try casting these values as strings instead of numbers since you’re obviously losing precision casting it as a number.

Tried string casting too but no luck!!

Can you try adding console logs to both things you are asserting so we can see what the values are?

console.log(jsonData.earnings.commissionLastWeek.amount);
console.log(data.earningscommissionLastWeekamount);

both have the same values:

Is that after casting either value?

Obviously those aren’t the exact same, so I’d expect the assertion to fail. Just not for the reason you’re seeing.

I already tried with casting and and it got failed as above error message.
Yes, those are not exact same due to some restriction from .csv file hence we are using include/match chai assertion for verification.

Anyway I already gave casting script for them as above, Do you have any other solution.

What is this data object you’re trying to assert on? If you’re just pulling out of a csv, I probably would have just done it this way:

const commissionLastWeek = pm.iterationData.get('earningscommissionLastWeekamount');
pm.expect(jsonData.earnings.commissionLastWeek.amount).to.equal(commissionLastWeek);

The only thing I can think of is that something is wrong with however you’re creating that data object.

const commissionLastWeek = pm.iterationData.get(‘earningscommissionLastWeekamount’);
pm.expect(jsonData.earnings.commissionLastWeek.amount).to.include(Number(commissionLastWeek));

Same an issue. @allen.helton You might be missing something but let me give something important in nutshell. Lets connect over screen share if possible, If required.

  1. Response contain value for earnings.commissionLastWeek.amount as “13490064.0000046”
    where as In .csv file I am putting same value as “13490064.0000046” but unfortunately file save value only integer value(13490064), It will not include decimal part.
    To overcome, In assertion we give “include” but its throw error
    pm.expect(jsonData.earnings.commissionLastWeek.amount).to.include(commissionLastWeek);

  2. Where as If we give equal/eql assertion type, Throw the correct error as
    pm.expect(jsonData.earnings.commissionLastWeek.amount).to.equal(commissionLastWeek);


    So many hence-forth, Can we connect over live screen sharing or any other idea?
    really looking forward!!

Oh, I see your problem, thank you for clarifying.

.includes doesn’t apply to numbers.

Instead, you could use Math.trunc if you really don’t care about the precision.

pm.expect(Math.trunc(jsonData.earnings.commissionLastWeek.amount)).to.equal(Math.trunc(commissionLastWeek));
1 Like

Thanks @allen.helton, Here is the my type of script and working fine without issues. pm.expect(Math.trunc(jsonData.earnings.commissionLastWeek.amount)).to.eql(data.earningscommissionLastWeekamount);

I will mark as solution but just for leaning and out of the postman related question who I can make .csv file data(100.45) with precision?

Issues with .csv file: In .csv file I am putting value as “13490064.0000046” but unfortunately file save value only integer value(13490064), It will not include decimal part.
Thought we can format it but once you close the file, values will reset and again no precision there.
Any suggestions?

It might have something to do with the program you’re using to manipulate the csv (like excel). But you could certainly try to surround the number in quotes to preserve the number as text, then you wouldn’t get any data loss.

Hi @aakashjain8693,

As @allen.helton noted, this is likely due to the client you are using to view the csv data. CSV file type is somewhat primitive (although very useful), meaning there is no context behind the data inferred. The clients used infer that, and likely in this case, Excel infers it to be an integer.

Best bet it so use quotes and they should be inferred as strings, leaving you with the precision you are looking for. This should work but again its dependent on the client being used to consume the csv. Though if you’re looking to do any numeric manipulations on code, you will have to cast it to the appropriate type (float, double, etc).

2 Likes

Perfect, Thanks!!
It might be the client type since unable to preserve the number as text through quotes.

Thanks @allen.helton @odanylewycz