Jasmine expect(resultCode).toBe(200 or 409)

2022-01-11 00:00:00 unit-testing jasmine javascript

对于某些测试场景,我需要针对多个值进行测试,这些值都可以.

For some test scenarios I run into the need of testing against multiple values which are all OK.

我想做的事情如下:

expect(resultCode).toBeIn([200,409]);

resultCode200409 时,此规范应通过.这可能吗?

This spec should pass when resultCode is either 200 or 409. Is that possible?

添加感谢 peter 和 dolarzo 指导我创建匹配器.我遇到了 addMatchers() 的问题.所以,最后我在 jasmine.js 中添加了以下内容:

ADDED Thanks to peter and dolarzo for pointing me towards creating matchers. I had problems with addMatchers(). So, in the end I added the following to the jasmine.js:

jasmine.Matchers.prototype.toBeIn = function (expected) {
    for (var i = 0; i < expected.length; i++)
        if (this.actual === expected[i])
            return true;
    return false;
};

这给了我一个可行的解决方案.我现在可以根据需要做 toBeIn.(茉莉花1.3.1)

This gave me a working solution. I can now do the toBeIn as needed. (Jasmine 1.3.1)

推荐答案

为了做这样的事情:

 expect(3).toBeIn([6,5,3,2]);

Jasmine 有一个称为匹配器的功能:

Jasmine has a feature called matchers:

这是一个关于如何声明它们的示例.我在最后声明了您正在寻找的方法:

This is an example on how to declare them. I have declared at the very end the method you are looking for:

describe('Calculator', function(){
    var obj;
    beforeEach(function(){
        //initialize object
        obj = new Object();

        jasmine.addMatchers({
            toBeFive: function () {
                return {
                    compare: function (actual, expected) {
                        return {
                            pass: actual === 5,
                            message: actual + ' is not exactly 5'
                        }
                    }
                };
            },
            toBeBetween: function (lower,higher) {
                return {
                    compare: function (actual, lower,higher) {
                        return {
                            pass: ( actual>= lower   && actual <= higher),
                            message: actual + ' is not between ' + lower + ' and ' + higher
                        }
                    }
                };
            },
            toBeIn: function(expected) {
                    return {
                        compare: function (actual, expected) {
                            return {
                                pass: expected.some(function(item){ return item === actual; }),
                                message: actual + ' is not in ' + expected
                            }
                        }
                    };
                }
        });


    });

这是你需要的匹配器:

toBeIn: function(expected) {
                    return {
                        compare: function (actual, expected) {
                            return {
                                pass: expected.some(function(item){ return item === actual; }),
                                message: actual + ' is not in ' + expected
                            }
                        }
                    };
                }

重要与 jasmine 2.0.我必须将 jasmine.addMatchers({ 与 jasmine specrunner.html 一起使用,但是当我使用 Karma 对其进行配置时,我必须将 jasmine 替换为 this 之类的this.addMatchers({ 因为 Karma 使用早期版本的 Jasmine.

Important with jasmine 2.0. I had to use jasmine.addMatchers({ with jasmine specrunner.html but when I configured it with Karma I had to replace jasmine with this like this.addMatchers({ because Karma use an earlier version of Jasmine.

相关文章