Javascript 强制失败 Jasmine 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32251887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:48:44  来源:igfitidea点击:

Force-failing a Jasmine test

javascriptjasmine

提问by Ben Aston

If I have code in a test that should never be reached (for example the failclause of a promise sequence), how can I force-fail the test?

如果我在测试中包含永远不会到达的代码(例如fail承诺序列的子句),我如何强制使测试失败?

I use something like expect(true).toBe(false);but this is not pretty.

我使用类似的东西,expect(true).toBe(false);但这并不漂亮。

The alternative is waiting for the test to timeout, which I want to avoid (because it is slow).

另一种方法是等待测试超时,我想避免这种情况(因为它很慢)。

回答by Michael Radionov

Jasmine provides a global method fail(), which can be used inside spec blocks it()and also allows to use custom error message:

Jasmine 提供了一个全局方法fail(),可以在规范块中使用it(),也允许使用自定义错误消息:

it('should finish successfully', function (done) {
  MyService.getNumber()
  .success(function (number) {
    expect(number).toBe(2);
    done();
  })
  .fail(function (err) {
    fail('Unwanted code branch');
  });
});

This is built-in Jasmine functionality and it works fine everywhere in comparison with the 'error' method I've mentioned below.

这是内置的 Jasmine 功能,与我在下面提到的“错误”方法相比,它在任何地方都可以正常工作。

Before update:

更新前:

You can throw an error from that code branch, it will fail a spec immediately and you'll be able to provide custom error message:

您可以从该代码分支抛出错误,它会立即使规范失败,您将能够提供自定义错误消息:

it('should finish successfully', function (done) {
  MyService.getNumber()
  .success(function (number) {
    expect(number).toBe(2);
    done();
  })
  .fail(function (err) {
    throw new Error('Unwanted code branch');
  });
});

But you should be careful, if you want to throw an error from Promise success handler then(), because the error will be swallowed in there and will never come up. Also you should be aware of the possible error handlers in your app, which might catch this error inside your app, so as a result it won't be able to fail a test.

但是你应该小心,如果你想从 Promise 成功处理程序中抛出错误then(),因为错误会被吞噬在那里并且永远不会出现。此外,您应该注意应用程序中可能的错误处理程序,它们可能会在您的应用程序中捕获此错误,因此它不会导致测试失败。

回答by John Henckel

Thanks TrueWillfor bringing my attention to this solution. If you are testing functions that return promises, then you should use the donein the it(). And instead of calling fail()you should call done.fail(). See Jasmine documentation.

感谢TrueWill让我注意到这个解决方案。如果您正在测试返回承诺的函数,那么您应该doneit(). 而不是打电话,fail()你应该打电话done.fail()。请参阅Jasmine 文档

Here is an example

这是一个例子

describe('initialize', () => {

    // Initialize my component using a MOCK axios
    let axios = jasmine.createSpyObj<any>('axios', ['get', 'post', 'put', 'delete']);
    let mycomponent = new MyComponent(axios);

    it('should load the data', done => {
        axios.get.and.returnValues(Promise.resolve({ data: dummyList }));
        mycomponent.initialize().then(() => {
            expect(mycomponent.dataList.length).toEqual(4);
            done();
        }, done.fail);     // <=== NOTICE
    });
});