te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Jasmine - How to test errors? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jasmine - How to test errors? - Stack Overflow

programmeradmin3浏览0评论

THE SITUATION:

Hello guys. I am learning jasmine to test my angular app.

I have create a basic function that does multiply two numbers. If the parameters given are not a number, the function throw an error.

I then made two very basic tests.

The first to check if the function properly multiply the numbers. The second to check if the function properly throw an error if a string is given as parameter.

The first test pass, the second not. And i don't understand why.

THE CODE:

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;
    }

}

The spec:

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"));

    });

});

THE OUTPUT:

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)

If i understand properly Jasmine. Both test should pass, because in the second case the function throw the error as we expected.

THE QUESTION:

How can i test if a function properly throw an error?



EDIT:

I am trying this new code, but is still not working:

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"));

    });

});

OUTPUT:

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.

THE SITUATION:

Hello guys. I am learning jasmine to test my angular app.

I have create a basic function that does multiply two numbers. If the parameters given are not a number, the function throw an error.

I then made two very basic tests.

The first to check if the function properly multiply the numbers. The second to check if the function properly throw an error if a string is given as parameter.

The first test pass, the second not. And i don't understand why.

THE CODE:

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;
    }

}

The spec:

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"));

    });

});

THE OUTPUT:

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)

If i understand properly Jasmine. Both test should pass, because in the second case the function throw the error as we expected.

THE QUESTION:

How can i test if a function properly throw an error?



EDIT:

I am trying this new code, but is still not working:

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"));

    });

});

OUTPUT:

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.
Share Improve this question edited Jan 10, 2015 at 12:01 FrancescoMussi asked Jan 10, 2015 at 11:49 FrancescoMussiFrancescoMussi 21.6k40 gold badges129 silver badges181 bronze badges 2
  • You need to run it in an anonymous function passed as "expect" argument. expect(function() { Multiply(10, 'aaaa') }) - otherwise jasmine doesn't have chance to capture it – zerkms Commented Jan 10, 2015 at 11:59
  • you would need .toThrowError(Error, "not a number"). Updated my answer. – lonelyelk Commented Nov 23, 2016 at 10:22
Add a ment  | 

3 Answers 3

Reset to default 7

If I understand correctly you need to pass a function into the expect(...) call.

The code you have here:

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

Is checking the result of Multiply, which when it works is fine, but like I said .toThrow() expects a function, I'd use an anonymous function instead, see below:

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

EDIT: Did a quick search and this blog post is a very detailed explanation of what I am trying to say.

You need to put the code you expect to throw an error into a function:

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

Otherwise, when you run your assertions, the error has already been thrown outside the scope. You can see available syntax for error matching in jasmine docs

  • The above methods are exactly true for exceptions handled inside the methods.
  • When you've to test the services you can use mocking mechanism to do that.
  • Use the advantage of NgModule providers section to create the mocks.

  • In describe() block,

providers: [{ provide: APIService, useValue: { api: { filterTaskDetails: () => throwError('Error') }}}]

throwError to be imported from rxjs.

  • In expect() block test it as,
spyOn(ponent['toaster'], 'showError');
/** add respected it() blocks**/
expect(ponent['toaster'].showError).toHaveBeenCalledTimes(1);
发布评论

评论列表(0)

  1. 暂无评论