简体   繁体   English

带有Mocha / Chai的条件if / else语句的单元测试

[英]Unit testing conditional if/else statements w/ Mocha/Chai

Quick question, I can not find any documentation online in regards to testing a simple function with several if/else statements such as this fizzBuzz example... 快速的问题,我无法在线找到关于使用多个if / else语句(例如此fizzBu​​zz示例)测试简单功能的文档。

module.exports =
function fizzBuzz(num) {
    for (let i; i <= num; i++) {
        if (i % 3 === 0 && i % 5 === 0) {
            return 'FIZZBUZZ';
        } else if (i % 3 === 0) {
            return 'FIZZ';
        } else if (i % 5 === 0) {
            return 'BUZZ';
        } else {
            return 'Your number is' + i;
        }
    }
}

Here is my Chai... 这是我的柴...

const assert = require('chai').assert;
const expect = require('chai').expect;
const fizzBuzz = require('../fizzBuzz.js');

describe.only('fizzBuzz', function () {
    describe('Smoke tests', function () {
        it('should exist', function () {
            expect(fizzBuzz).to.exist;
        })
        it('Should be a function', function() {
            expect(fizzBuzz).to.be.a('function');
        })
    })
})

What would be the syntax for checking each if/else statement? 检查每个if / else语句的语法是什么? I would like to check to make sure on certain conditions, the function returns a string, and others returns a number. 我想检查以确保在某些情况下,该函数返回一个字符串,而其他返回一个数字。 As well as when it returns a string, does it match which word should be returned. 以及返回字符串时,它是否匹配应返回的单词。 I am very new to Mocha/Chai. 我对摩卡/柴很陌生。

Thank you in advance guys, 预先谢谢你们,

Brittany 布列塔尼

For example: 例如:

it('Pass param 5 should return \'BUZZ\'', function() {
    expect(fizzBuzz(5)).to.equal('BUZZ');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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