简体   繁体   中英

Testing an invalid argument w/ Chai in JavaScript

Preface: Last week I started tinkering with JavaScript, so this may or may not be a dumb question (though I'll wager the former).

I created this method to return the day of the week when getDayOfTheWeekFromDate is initialized with a Date() . I'm trying to create a test with chai to see what it does when it's fed a bogus argument (like a string).

Two questions:

  1. Am I checking the type correctly?
  2. How would I test a bogus argument for getDayOfTheWeekFromDate with chai ?

     DateHelper.prototype.getDayOfTheWeekFromDate = function(inputDate) { if (inputDate instanceof Date) { inputDate = new Date(); var dayOfTheWeek = inputDate.getDay(); switch(dayOfTheWeek) { case 0: return "Sunday"; case 1: return "Monday"; case 2: return "Tuesday"; case 3: return "Wednesday"; case 4: return "Thursday"; case 5: return "Friday"; case 6: return "Saturday"; default: return; } } else { // I'm shooting for doing nothing if the input isn't a Date() // How would I test this? return; } }; 

I've got a test class set up to test a Date helper class.

'use strict';
var chai = require('chai');
var expect = chai.expect;
var DateHelper = require('../date_helper');
chai.config.includeStack = true; // true enables stack trace

describe('DateHelper', function() {

    context('With a valid date value, getDayOfTheWeekFromDate', function() {
        it('should return Tuesday', function() {
            expect(subject.getDayOfTheWeekFromDate(new Date())).to.eq('Tuesday');
        });
    });
});

I tried this, but the console wigs out when I feed the initializer a string, doesn't know what undefined is. What should I be testing here?:

context('With a invalid date value, getDayOfTheWeekFromDate', function() {
    it('should return nothing', function() {
        expect(subject.getDayOfTheWeekFromDate('bogusArgument').to.be.undefined;
    });
});

It would work fine if you had all the parentheses balanced:

expect(subject.getDayOfTheWeekFromDate('bogusArgument')).to.be.undefined;
                            //                         ^
                            //    this one was missing /

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM