javascript中面临的错误

javascript中面临的错误,javascript,javascript-events,karma-runner,karma-jasmine,Javascript,Javascript Events,Karma Runner,Karma Jasmine,我是测试自动化的新手,我尝试使用karma和jasmine框架自动化测试用例,但由于某些原因,我遇到了javascript错误。我通过创建计算器添加功能来模拟测试用例。这是我的档案 计算器功能文件: calculator.js: window.calculator = window.calculator || {}; (function () { var getIntById = function (id) { return parseInt(document.getE

我是测试自动化的新手,我尝试使用karma和jasmine框架自动化测试用例,但由于某些原因,我遇到了javascript错误。我通过创建计算器添加功能来模拟测试用例。这是我的档案

计算器功能文件: calculator.js:

window.calculator = window.calculator || {};

(function () {
    var getIntById = function (id) {
        return parseInt(document.getElementById(id).value, 10);
    };

    var calculate = function () {
        var sum = getIntById('x') + getIntById('y');
        document.getElementById('result').innerHTML = isNaN(sum) ? 0 : sum;
    };

    window.calculator.init = function () {
        document.getElementById('add').addEventListener('click', calculate);
    };
})();
我的测试用例: calculator.test.js

describe('Calculator', function () {

    // inject the HTML fixture for the tests
    beforeEach(function () {
        var fixture = '<input id="x" type="text">' +
          '<input id="y" type="text">' +
          '<input id="add" type="button" value="Add Numbers">' +
          'Result: <span id="result" />';

        document.body.insertAdjacentHTML(
          'afterbegin',
          fixture);
    });

    // remove the html fixture from the DOM
    afterEach(function () {
        document.body.removeChild(document.getElementById('fixture'));
    });

    // call the init function of calculator to register DOM elements
    beforeEach(function () {
        window.calculator.init();
    });

    it('should return 3 for 1 + 2', function () {
        document.getElementById('x').value = 1;
        document.getElementById('y').value = 2;
        document.getElementById('add').click();
        expect(document.getElementById('result').innerHTML).toBe('3');
    });

    it('should calculate zero for invalid x value', function () {
        document.getElementById('x').value = 'hello';
        document.getElementById('y').value = 2;
        document.getElementById('add').click();
        expect(document.getElementById('result').innerHTML).toBe('0');
    });

    it('should calculate zero for invalid y value', function () {
        document.getElementById('x').value = 1;
        document.getElementById('y').value = 'goodbye';
        document.getElementById('add').click();
        expect(document.getElementById('result').innerHTML).toBe('0');
    });

});
描述('Calculator',函数(){
//为测试注入HTML夹具
beforeach(函数(){
变量fixture=''+
'' +
'' +
“结果:”;
document.body.insertAdjacentHTML(
“后开始”,
固定装置);
});
//从DOM中删除html设备
在每个(函数()之后){
document.body.removeChild(document.getElementById('fixture');
});
//调用计算器的init函数注册DOM元素
beforeach(函数(){
window.calculator.init();
});
它('应该为1+2'返回3',函数(){
document.getElementById('x')。值=1;
document.getElementById('y')。值=2;
document.getElementById('add')。单击();
expect(document.getElementById('result').innerHTML).toBe('3');
});
它('应该为无效的x值计算零',函数(){
document.getElementById('x')。value='hello';
document.getElementById('y')。值=2;
document.getElementById('add')。单击();
expect(document.getElementById('result').innerHTML).toBe('0');
});
它('应该为无效的y值计算零',函数(){
document.getElementById('x')。值=1;
document.getElementById('y')。值='再见';
document.getElementById('add')。单击();
expect(document.getElementById('result').innerHTML).toBe('0');
});
});
我得到以下错误:

09 01 2016 17:25:16.329:INFO [Chrome 47.0.2526 (Windows 8.1 0.0.0)]: Connected o
n socket /#vtPxHSF4qNGPq7ABAAAA with id 24482327
Chrome 47.0.2526 (Windows 8.1 0.0.0) Calculator should return 3 for 1 + 2 FAILED

        TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not
 of type 'Node'.
            at TypeError (native)
            at Object.<anonymous> (C:/Users/rapandey/Documents/Visual Studio 201
5/Projects/demo1/demo1/test/calculator.test.js:17:23)
Chrome 47.0.2526 (Windows 8.1 0.0.0): Executed 1 of 3 (1 FAILED) (0 secs / 0.006
Chrome 47.0.2526 (Windows 8.1 0.0.0) Calculator should calculate zero for invali
d x value FAILED
        TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not
 of type 'Node'.
            at TypeError (native)
            at Object.<anonymous> (C:/Users/rapandey/Documents/Visual Studio 201
5/Projects/demo1/demo1/test/calculator.test.js:17:23)
Chrome 47.0.2526 (Windows 8.1 0.0.0): Executed 2 of 3 (2 FAILED) (0 secs / 0.007
Chrome 47.0.2526 (Windows 8.1 0.0.0) Calculator should calculate zero for invali
d y value FAILED
        TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not
 of type 'Node'.
            at TypeError (native)
            at Object.<anonymous> (C:/Users/rapandey/Documents/Visual Studio 201
5/Projects/demo1/demo1/test/calculator.test.js:17:23)
Chrome 47.0.2526 (Windows 8.1 0.0.0): Executed 3 of 3 (3 FAILED) (0 secs / 0.007
Chrome 47.0.2526 (Windows 8.1 0.0.0): Executed 3 of 3 (3 FAILED) ERROR (0.033 se
cs / 0.007 secs)
09 01 2016 17:25:16.329:INFO[Chrome 47.0.2526(Windows 8.1 0.0.0)]:已连接到
n插座/#vtPxHSF4qNGPq7ABAAAA,id为24482327
Chrome 47.0.2526(Windows 8.1 0.0.0)计算器应为1+2返回3失败
TypeError:未能在“节点”上执行“removeChild”:参数1不可用
类型为“节点”。
at TypeError(本机)
反对。(C:/Users/rapandey/Documents/visualstudio 201
5/Projects/demo1/demo1/test/calculator.test.js:17:23)
Chrome 47.0.2526(Windows 8.1 0.0.0):执行3次中的1次(1次失败)(0秒/0.006)
Chrome 47.0.2526(Windows 8.1 0.0.0)计算器应计算零表示无效
d x值失败
TypeError:未能在“节点”上执行“removeChild”:参数1不可用
类型为“节点”。
at TypeError(本机)
在对象处。(C:/Users/rapandey/Documents/visualstudio 201
5/Projects/demo1/demo1/test/calculator.test.js:17:23)
Chrome 47.0.2526(Windows 8.1 0.0.0):执行3次中的2次(2次失败)(0秒/0.007
Chrome 47.0.2526(Windows 8.1 0.0.0)计算器应计算零表示无效
d y值失败
TypeError:未能在“节点”上执行“removeChild”:参数1不可用
类型为“节点”。
at TypeError(本机)
在对象处。(C:/Users/rapandey/Documents/visualstudio 201
5/Projects/demo1/demo1/test/calculator.test.js:17:23)
Chrome 47.0.2526(Windows 8.1 0.0.0):执行3次(3次失败)(0秒/0.007)
Chrome 47.0.2526(Windows 8.1 0.0.0):执行3次(3次失败)错误(0.033 se
cs/0.007秒)

无法找出我犯了什么错误。

您的问题在于每次实施后的
。您删除了一个不存在的元素:

afterEach(function () {
    document.body.removeChild(document.getElementById('fixture'));
});
但您的HTML只是:

<input id="x" type="text">
<input id="y" type="text">
<input id="add" type="button" value="Add Numbers">
Result: <span id="result" />

结果:

您的问题在于每次实施后的
。您删除了一个不存在的元素:

afterEach(function () {
    document.body.removeChild(document.getElementById('fixture'));
});
但您的HTML只是:

<input id="x" type="text">
<input id="y" type="text">
<input id="add" type="button" value="Add Numbers">
Result: <span id="result" />

结果:

好的,我也是测试框架的新手。我想的是“每个之前”html内容将被添加,而“每个之后”html内容将被删除。这意味着在每个之后执行之前,html内容将已经形成。我走对了吗?好的,我也是测试框架的新手。我想的是“每个之前”html内容将被添加和'afterEach'将被删除。这意味着在执行afterEach之前,html内容将已经形成。我的路径正确吗?