csdnceshi62 2010-11-10 12:57 采纳率: 100%
浏览 307
已采纳

如何编写一个期望 Jasmine 出错的测试?

I'm trying to write a test for the Jasmine Test Framework which expects an error. At the moment I'm using a Jasmine Node.js integration from GitHub.

In my Node module I have the following code:

throw new Error("Parsing is not possible");

Now I try to write a test which expects this error:

describe('my suite...', function() {
    [..]
    it('should not parse foo', function() {
    [..]
        expect(parser.parse(raw)).toThrow(new Error("Parsing is not possible"));
    });
});

I tried also Error() and some other variants and just can't figure out how to make it work.

转载于:https://stackoverflow.com/questions/4144686/how-to-write-a-test-which-expects-an-error-to-be-thrown-in-jasmine

  • 写回答

8条回答 默认 最新

  • 狐狸.fox 2010-11-10 13:13
    关注

    you should be passing a function into the expect(...) call. The code you have here:

    // incorrect:
    expect(parser.parse(raw)).toThrow(new Error("Parsing is not possible"));
    

    is trying to actually call parser.parse(raw) in an attempt to pass the result into expect(...),

    Try using an anonymous function instead:

    expect( function(){ parser.parse(raw); } ).toThrow(new Error("Parsing is not possible"));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效