Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.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
Javascript 茉莉花-如何测试错误?_Javascript_Angularjs_Unit Testing_Jasmine - Fatal编程技术网

Javascript 茉莉花-如何测试错误?

Javascript 茉莉花-如何测试错误?,javascript,angularjs,unit-testing,jasmine,Javascript,Angularjs,Unit Testing,Jasmine,情况: function Multiply( num1, num2 ) { var result; if (isNaN(num1) || isNaN(num2)) { throw new Error("not a number"); } else { result = num1 * num2; return result; } } describe('The function',

情况:

function Multiply( num1, num2 )
{

    var result;

    if (isNaN(num1) || isNaN(num2)) 
    {
        throw new Error("not a number");
    }
    else
    {
        result = num1 * num2;

        return result;
    }

}
describe('The function', function () 
{
    it('properly multiply two numbers', function () 
    {
        result = Multiply(10, 5);
        expect(result).toEqual(50);
    });

    it('throw an error if a parameter is not a number', function () 
    {
        result = Multiply(10, 'aaaa');

        expect(result).toThrow(new Error("not a number"));

    });

});
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: not a number
Error: not a number
    at Multiply (http://localhost/jasmine_test/src/app.js:8:9)
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: Expected is not an Error, string, or RegExp.
大家好。我正在学习jasmine来测试我的angular应用程序

我已经创建了一个基本函数,它将两个数相乘。 如果给定的参数不是数字,函数将抛出错误

然后我做了两个非常基本的测试

首先检查函数是否正确地将数字相乘。 第二种方法是检查如果字符串作为参数给出,函数是否正确抛出错误

第一次测试通过,第二次不通过。我不明白为什么

代码:

function Multiply( num1, num2 )
{

    var result;

    if (isNaN(num1) || isNaN(num2)) 
    {
        throw new Error("not a number");
    }
    else
    {
        result = num1 * num2;

        return result;
    }

}
describe('The function', function () 
{
    it('properly multiply two numbers', function () 
    {
        result = Multiply(10, 5);
        expect(result).toEqual(50);
    });

    it('throw an error if a parameter is not a number', function () 
    {
        result = Multiply(10, 'aaaa');

        expect(result).toThrow(new Error("not a number"));

    });

});
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: not a number
Error: not a number
    at Multiply (http://localhost/jasmine_test/src/app.js:8:9)
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: Expected is not an Error, string, or RegExp.
功能:

function Multiply( num1, num2 )
{

    var result;

    if (isNaN(num1) || isNaN(num2)) 
    {
        throw new Error("not a number");
    }
    else
    {
        result = num1 * num2;

        return result;
    }

}
describe('The function', function () 
{
    it('properly multiply two numbers', function () 
    {
        result = Multiply(10, 5);
        expect(result).toEqual(50);
    });

    it('throw an error if a parameter is not a number', function () 
    {
        result = Multiply(10, 'aaaa');

        expect(result).toThrow(new Error("not a number"));

    });

});
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: not a number
Error: not a number
    at Multiply (http://localhost/jasmine_test/src/app.js:8:9)
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: Expected is not an Error, string, or RegExp.
规范:

function Multiply( num1, num2 )
{

    var result;

    if (isNaN(num1) || isNaN(num2)) 
    {
        throw new Error("not a number");
    }
    else
    {
        result = num1 * num2;

        return result;
    }

}
describe('The function', function () 
{
    it('properly multiply two numbers', function () 
    {
        result = Multiply(10, 5);
        expect(result).toEqual(50);
    });

    it('throw an error if a parameter is not a number', function () 
    {
        result = Multiply(10, 'aaaa');

        expect(result).toThrow(new Error("not a number"));

    });

});
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: not a number
Error: not a number
    at Multiply (http://localhost/jasmine_test/src/app.js:8:9)
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: Expected is not an Error, string, or RegExp.
输出:

function Multiply( num1, num2 )
{

    var result;

    if (isNaN(num1) || isNaN(num2)) 
    {
        throw new Error("not a number");
    }
    else
    {
        result = num1 * num2;

        return result;
    }

}
describe('The function', function () 
{
    it('properly multiply two numbers', function () 
    {
        result = Multiply(10, 5);
        expect(result).toEqual(50);
    });

    it('throw an error if a parameter is not a number', function () 
    {
        result = Multiply(10, 'aaaa');

        expect(result).toThrow(new Error("not a number"));

    });

});
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: not a number
Error: not a number
    at Multiply (http://localhost/jasmine_test/src/app.js:8:9)
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: Expected is not an Error, string, or RegExp.
如果我理解正确的话,茉莉。这两个测试都应该通过,因为在第二种情况下,函数会像我们预期的那样抛出错误

问题:

function Multiply( num1, num2 )
{

    var result;

    if (isNaN(num1) || isNaN(num2)) 
    {
        throw new Error("not a number");
    }
    else
    {
        result = num1 * num2;

        return result;
    }

}
describe('The function', function () 
{
    it('properly multiply two numbers', function () 
    {
        result = Multiply(10, 5);
        expect(result).toEqual(50);
    });

    it('throw an error if a parameter is not a number', function () 
    {
        result = Multiply(10, 'aaaa');

        expect(result).toThrow(new Error("not a number"));

    });

});
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: not a number
Error: not a number
    at Multiply (http://localhost/jasmine_test/src/app.js:8:9)
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: Expected is not an Error, string, or RegExp.
如何测试函数是否正确抛出错误



编辑:

function Multiply( num1, num2 )
{

    var result;

    if (isNaN(num1) || isNaN(num2)) 
    {
        throw new Error("not a number");
    }
    else
    {
        result = num1 * num2;

        return result;
    }

}
describe('The function', function () 
{
    it('properly multiply two numbers', function () 
    {
        result = Multiply(10, 5);
        expect(result).toEqual(50);
    });

    it('throw an error if a parameter is not a number', function () 
    {
        result = Multiply(10, 'aaaa');

        expect(result).toThrow(new Error("not a number"));

    });

});
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: not a number
Error: not a number
    at Multiply (http://localhost/jasmine_test/src/app.js:8:9)
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: Expected is not an Error, string, or RegExp.
我正在尝试此新代码,但仍然不起作用:

describe('The function', function () 
{

    it('throw an error if a parameter is not a number', function () 
    {

        expect(function() { Multiply(10, 'aaaa') }).toThrowError(new Error("not a number"));

    });

});
输出:

function Multiply( num1, num2 )
{

    var result;

    if (isNaN(num1) || isNaN(num2)) 
    {
        throw new Error("not a number");
    }
    else
    {
        result = num1 * num2;

        return result;
    }

}
describe('The function', function () 
{
    it('properly multiply two numbers', function () 
    {
        result = Multiply(10, 5);
        expect(result).toEqual(50);
    });

    it('throw an error if a parameter is not a number', function () 
    {
        result = Multiply(10, 'aaaa');

        expect(result).toThrow(new Error("not a number"));

    });

});
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: not a number
Error: not a number
    at Multiply (http://localhost/jasmine_test/src/app.js:8:9)
2 specs, 1 failure
Spec List | Failures
The function throw an error if a parameter is not a number
Error: Expected is not an Error, string, or RegExp.

如果我理解正确,您需要将函数传递到
expect(…)
调用中

您在此处拥有的代码:

expect(result).toThrow(new Error("not a number"));
正在检查乘法的结果,当它工作时,这是正常的,但正如我所说的。toThrow()需要一个函数,我将使用匿名函数,请参见以下内容:

expect( function(){ Multiply(10, 'aaaa'); } ).toThrow(new Error("not a number"));

编辑:进行了快速搜索,博客文章对我想说的内容进行了非常详细的解释。

您需要将您希望在函数中抛出错误的代码放入:

expect(function () {
    Multiply(10, 'aaaa');
}).toThrow(Error, 'not a number');
否则,当您运行断言时,错误已经抛出范围之外。您可以在

  • 对于方法内部处理的异常,上述方法完全正确
  • 当您必须测试服务时,您可以使用模拟机制进行测试
  • 使用NgModule providers部分的优势创建模拟

  • 在descripe()块中

providers:[{provide:APIService,useValue:{api:{filterTaskDetails:()=>throwError('Error')}]
要从
rxjs
导入的
throwerr

  • 在expect()中,将其块测试为
spyOn(组件['toaster'],'showError');
/**添加()块**/
预期(组件['toaster'].淋浴器)已提前(1);

您需要在作为“expect”参数传递的匿名函数中运行它
expect(function(){Multiply(10,'aaaa')})
-否则jasmine没有机会捕获它,您需要
。tothrower(错误,“不是数字”)
。更新了我的答案。也可以使用这个
expect(()=>{Multiply(10,'aaaa');})