谁还没个明天 2010-11-10 12:57 采纳率: 50%
浏览 283
已采纳

如何编写一个期望 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条回答 默认 最新

  • YaoRaoLov 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条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100