[英]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). 前言:上周,我开始修改JavaScript,因此这可能是(也可能不是)一个愚蠢的问题(尽管我会押注前者)。
I created this method to return the day of the week when getDayOfTheWeekFromDate
is initialized with a Date()
. 我创建了此方法,以使用
Date()
初始化getDayOfTheWeekFromDate
时返回星期几。 I'm trying to create a test with chai
to see what it does when it's fed a bogus argument (like a string). 我正在尝试使用
chai
创建一个测试,以了解它在输入伪造的参数(如字符串)时会做什么。
Two questions: 两个问题:
How would I test a bogus argument for getDayOfTheWeekFromDate
with chai
? 我将如何使用
chai
为getDayOfTheWeekFromDate
测试伪造的参数?
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. 我已经建立了一个测试类来测试Date助手类。
'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. 我试过了,但是当我向初始化器输入一个字符串时,控制台会显示出来,但不知道undefined是什么。 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 /
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.