Jest.js 如何测试依赖于变量的函数?

hiz5n14c  于 2022-12-08  发布在  Jest
关注(0)|答案(2)|浏览(195)

我正在尝试对下面的函数进行单元测试。正如你所看到的,函数的输出依赖于count的值。什么是测试这种类型函数的适当语法和函数?我对Jest和javascript非常陌生。

function greetings(){
    if(count == 0){ 
        return "Hello! I am here to assist you in picking your ideal Apple product! YAYYY :D! We can start off by selecting the type of Apple product you wish to buy";
    }else if(count == 1){
        return "Hello again! Once we get your budget I can narrow down options for your ideal product! Whats your max budget?";
    }else if(count == 2){
        return "Hello again my friend! Once we get your ideal device size I can narrow down options for your ideal product! Whats your ideal size for this device?";
    }
}

这就是我所尝试的

test ('greetings message test', () => {
    expect(responses.greetings().toBe("Hello! I am here to assist you in picking your ideal Apple product! YAYYY :D! We can start off by selecting the type of Apple product you wish to buy" || "Hello again! Once we get your budget I can narrow down options for your ideal product! Whats your max budget?" || "Hello again my friend! Once we get your ideal device size I can narrow down options for your ideal product! Whats your ideal size for this device?"))

});

测试失败了。我不确定正确的方法是什么。如果能得到帮助,我将不胜感激。

eyh26e7m

eyh26e7m1#

有多种方法。
第一个是添加一个易于测试的纯函数

var count = 0;

export function increment(){
  count++;
}

export function decrement(){
 count--;
}

export function greetings() {
  return getGreetings(count);
}

// function to test
export function getGreetings(c) {
    if(c == 0){ 
        return "Hello! I am here to assist you in picking your ideal Apple product! YAYYY :D! We can start off by selecting the type of Apple product you wish to buy";
    }else if(c == 1){
        return "Hello again! Once we get your budget I can narrow down options for your ideal product! Whats your max budget?";
    }else if(c == 2){
        return "Hello again my friend! Once we get your ideal device size I can narrow down options for your ideal product! Whats your ideal size for this device?";
    }
}

第二种方法是将模块作为一个整体进行测试(不那么单一),可以通过在每次测试中重置模块并调用递增/递减函数(或者代码中可以更改计数的任何函数)来将计数设置为预期值

describe('responses', () => {
    beforeEach(() => {
        // this will reset the module so when you use
        // const responses = require('./responses') it will get a fresh responses module with count = 0
        jest.resetModules()
    });

    it('test count 0', () => {
        const module = require('./responses');
        expect(module.greetings()).toBe('greeting for count 0');
    })
    it('test count 1', () => {
        const module = require('./responses');
        module.increment();
        expect(module.greetings()).toBe('greeting for count 1');
    })
    it('test count 2', () => {
        const module = require('./responses');
        module.increment();
        module.increment();
        expect(module.greetings()).toBe('greeting for count 2');
    })
    
})
gcmastyq

gcmastyq2#

首先我强烈建议其他用户已经说过的(例如,添加一个实际上将计数作为参数的纯函数,这样您就可以轻松地测试它)BUT如果由于未知原因您不能严格更改var count外部的实现,我的建议是以这种方式稍微更改greetings的实现:

function greetings(_c){
    var innerCount = _c || count;
    if(innerCount === 0){ 
        return "Hello! I am here to assist you in picking your ideal Apple product! YAYYY :D! We can start off by selecting the type of Apple product you wish to buy";
    } else if(innerCount === 1){
        return "Hello again! Once we get your budget I can narrow down options for your ideal product! Whats your max budget?";
    } else if(innerCount === 2){
        return "Hello again my friend! Once we get your ideal device size I can narrow down options for your ideal product! Whats your ideal size for this device?";
    }
}

然后你可以在测试中使用类似这样的东西:

expect(responses.greetings(0).toBe("Hello!...."))

相关问题