Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何使用jsdom对页面重定向进行单元测试,localhost是';空白';_Unit Testing_Redirect_Mocha.js_Chai_Jsdom - Fatal编程技术网

Unit testing 如何使用jsdom对页面重定向进行单元测试,localhost是';空白';

Unit testing 如何使用jsdom对页面重定向进行单元测试,localhost是';空白';,unit-testing,redirect,mocha.js,chai,jsdom,Unit Testing,Redirect,Mocha.js,Chai,Jsdom,我有一个功能,它的目的是重定向到pageA,如果由用户触发,否则重定向到pageB 当我对函数进行单元测试时 const expected = `${host}/pageA`; redirect(true); // calling the redirect function const actual = window.location; expect(actual).to.equal(expected); 失败,因为“blank

我有一个功能,它的目的是重定向到pageA,如果由用户触发,否则重定向到pageB

当我对函数进行单元测试时

        const expected = `${host}/pageA`;

        redirect(true); // calling the redirect function
        const actual = window.location;
        expect(actual).to.equal(expected);
失败,因为“blank/pageA”不是“”

看起来window.location有一个“空白”主机,为什么会发生这种情况?我该如何解决这个问题? 我正在使用jsdom、摩卡、柴

我的设置如下所示:

    let originalWindow = global.window,
    host = 'https://localhost:5401';

    beforeEach(() => {
        global.window = {
            location: {
                href: "https://localhost:5401/pageA/2020"
            }
        };
    });

    afterEach(() => {
        global.window = originalWindow;
    });
请把这个做成一个盒子。问题可能在您显示的代码之外的其他地方。请将此问题转化为一个问题。问题可能出现在代码之外的其他地方。