异步测试功能

时间:2018-10-07 00:07:24

标签: javascript unit-testing asynchronous testing

我正在尝试创建一个函数来测试异步代码,但是我有点迷失了,我希望下面的TEST_F和TEST函数也可以与异步代码一起使用,例如加载图像。

const CHECK = (actual, expected) => {
    return (actual === expected);
};

const TEST = (name, ...testFunctions) => {
    console.log(name+':');
    for (let test of testFunctions)
        console.log(test);
};

const TEST_F = (name, f) => {
    const tStart = performance.now();

    const check = f();

    const tEnd = performance.now();
    const duration = tEnd - tStart;

    const details = name + ': ' + '(' + duration + ') = ' + check;

    return details;
};

const imageDownload = (path, successCallback) => {
    let img = new Image();

    img.addEventListener("load", successCallback, false);

    img.src = path;

    return img;
};

TEST("TestImage", 
    TEST_F("testImageDownload", () => {
        let spyCountSuccess = 0;
        const expectedCountSuccess = spyCountSuccess + 1;

        const successCallback = () => {
            spyCountSuccess++;
        };
        const pathImage = 'https://i.imgur.com/Wutekcp.jpg';
        imageDownload(pathImage, successCallback);

        const actualCountSuccess = spyCountSuccess;

        return CHECK(actualCountSuccess, expectedCountSuccess);
    })

);

使用上面的代码,即使加载了图像,我也总是会出错,因为我没有正确处理异步概念,所以我想了解如何修改代码,从而也可以测试ascincrono代码。

1 个答案:

答案 0 :(得分:0)

经过一些测试,我能够测试异步代码。我不知道是否会保持这种状态,但是它可以正常工作,我将尝试研究这些内容,看看有什么可以改进的地方:

const CHECK = (actual, expected) => {
    return (actual === expected);
};

const TEST = (name, ...testFunctions) => {
    console.log(name+':');
    for (let test of testFunctions)
        if(test === undefined)
            continue;
        else
            console.log(test);
};

const TEST_F = (name, f) => {
    const tStart = performance.now();
    const check = f();
    const tEnd = performance.now();
    const duration = tEnd - tStart;
    
    const details = name + ': ' + '(' + duration + ') = ' + check;
  
    return details;
};

const TEST_F_ASYNC = (name, f) => {
    const tStart = performance.now();
    f((check) => {
      const tEnd = performance.now();
      const duration = tEnd - tStart;
      
      const details = name + ': ' + '(' + duration + ') = ' + check;
    
      console.log(details);
    });
    
};

const imageDownload = (path, successCallback) => {
    let img = new Image();
    
    img.addEventListener("load", successCallback, false);
    
    img.src = path;
    
    return img;
};

TEST("TestImage", 
    TEST_F_ASYNC("testAsync", (done) => {
        let spyCountSuccess = 0;
        const expectedCountSuccess = spyCountSuccess + 1;
        
        const successCallback = () => {
            spyCountSuccess++;
            const actualCountSuccess = spyCountSuccess;
            const result = CHECK(actualCountSuccess, expectedCountSuccess)
            done(result);
        };
        const pathImage = 'https://i.imgur.com/Wutekcp.jpg';
        imageDownload(pathImage, successCallback);
        
    }),
    
    TEST_F('testSync', () => {
        return CHECK(1, 1);
    })

);