Get help with testing, discuss unit testing strategies etc.


Post by nuridesengin »

I've a function which is simply interval the given date;
dateAdd: function (theDate, theInterval, theValue) {
        var me = this;


        theDate = theDate || me.today();
        theInterval = theInterval || Ext.Date.DAY;
        theValue = theValue || 1;


        return Ext.Date.add(theDate, theInterval, theValue);
    },
but it gives error! Even when i gave exactly the same date/time/timezone value. Why could i getting this error?
t.describe('dateAdd() method', function(t) {
        t.it('should interval the given date', function(t) {
            t.expect(MyApp.Globals.dateAdd(new Date('01.02.2018'), Ext.Date.DAY, 1)).toBe('Wed Jan 03 2018 00:00:00 GMT+0300 (+03)');
        });
    });
Launching test suite, OS: MacOS, browser: Chrome 63.0.3239.132
dateAdd() method
    should interval the given date
        fail 1 - Expect "Wed Jan 03 2018 00:00:00 GMT+0300 (+03)" to be "Wed Jan 03 2018 00:00:00 GMT+0300 (+03)"
        Failed assertion `expect(got).toBe(need)` at line 69 of 01-unit-tests/010_globals.t.js
        Got  : "Wed Jan 03 2018 00:00:00 GMT+0300 (+03)"
        Need : "Wed Jan 03 2018 00:00:00 GMT+0300 (+03)"
[FAIL]  01-unit-tests/010_globals.t.js

Post by nickolay »

You compare Date with String - thats why.

Post by nuridesengin »

Ok. sorry to miss that =| but it's not match with a new date method as well;
t.describe('dateAdd() method', function(t) {
        t.it('should interval the given date', function(t) {
            t.expect(MyApp.Globals.dateAdd(new Date('01.01.2018'),Ext.Date.DAY, 1)).toBe(new Date('01.02.2018'));
        });
    });
Expect "Tue Jan 02 2018 00:00:00 GMT+0300 (+03)" to be "Tue Jan 02 2018 00:00:00 GMT+0300 (+03)"
Failed assertion `expect(got).toBe(need)` at line 24 of 01-unit-tests/010_globals.t.js
Got  : "Tue Jan 02 2018 00:00:00 GMT+0300 (+03)"
Need : "Tue Jan 02 2018 00:00:00 GMT+0300 (+03)"

Post by nickolay »

Hm.. This seems to be a bug, thanks for pointing! Right now, please use explicit cast to number or to string:
t.it('should interval the given date', function(t) {
            t.expect(MyApp.Globals.dateAdd(new Date('01.01.2018'),Ext.Date.DAY, 1) - 0).toBe(new Date('01.02.2018') - 0);
        });

t.it('should interval the given date', function(t) {
            t.expect(MyApp.Globals.dateAdd(new Date('01.01.2018'),Ext.Date.DAY, 1) + '').toBe(new Date('01.02.2018') + '');
        });
I'll fix this today, will be available in the tomorrow nightly build.

Post by nickolay »

Or use "t.is()"

Post by nickolay »

Actually, this is by design (following jasmine conventions). "toBe" is equivalent of ===, which will always be `false` for different object instances ({} === {} - false). Since Date is an object it fails.

You want "toEqual()" instead.

I'll update the docs, to mention the Date explicitly.

Post by nuridesengin »

toEqual matcher is work pretty fine! Thanks for advice

Post Reply