Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

javascript - Loose equality in Mocha

In my test, I am passing an object that has two boolean values (among other values) to my API. I am then testing that the object was stored correctly in my database. The response should be exactly the same as the input, except that MySql stores booleans as integers.

I would like to test all of the fields without repetitive code, so I have written this:

const validChannel = {
    id: `${new Date().getTime()}`,
    major_nbr: 2,
    minor_nbr: 1,
    delivery: 'ATSC1',
    display_nbr: '2.10',
    opt_in: true,
    visible: true,
    callsign: 'TEST',
};

const channel = api.get();
Object.keys(validChannel).forEach((k) => {
   expect(channel[k]).to.equal(validChannel[k]);
});

This works great, except that Mocha uses strict comparison, so 1 != true. How can I make this work while still keeping it compact?

question from:https://stackoverflow.com/questions/65853259/loose-equality-in-mocha

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If the mountain will not come to Muhammad...

If Mocha can't loose equality, then let the work to JS. Don't use mocha equals, instead use == JS

Look this example where all values displayed by console are true. Of course here I can't use mocha assertions but the response will be true too.

const validChannel = {
    id: `${new Date().getTime()}`,
    major_nbr: 2,
    minor_nbr: 1,
    delivery: 'ATSC1',
    display_nbr: '2.10',
    opt_in: true,
    visible: true,
    callsign: 'TEST',
};
//here is your call
const channel = Object.assign({}, validChannel)
channel.opt_in = 1;
channel.visible = 1;

Object.keys(validChannel).forEach((k) => {
   //expect(channel[k] == validChannel[k]).to.equal(true);
   console.log(channel[k] == validChannel[k])
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...