Jasmine expect(resultCode).toBe(200或409) [英] Jasmine expect(resultCode).toBe(200 or 409)

查看:461
本文介绍了Jasmine expect(resultCode).toBe(200或409)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于某些测试场景,我遇到了针对多个值进行测试的需要,这些值都可以。

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

我想做的事情如下:

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

resultCode 200 409 。这可能吗?

ADDED
感谢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。 (Jasmine 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有一项名为matchers的功能:

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.addMatchers({和jasmine specrunner.html但是当我用Karma配置它时我必须用这个替换 jasmine 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.

这篇关于Jasmine expect(resultCode).toBe(200或409)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆