Jasmine - 任何布尔值(jasmine.any(布尔))

时间:2021-12-04 07:43:20

I write unit tests for angular using karma, jasmine. Try to write:

我用karma,jasmine编写角度的单元测试。试着写:

expect(item).toEqual(jasmine.any(Boolean));

but got:

但得到了:

Expected true to equal <jasmine.any(function Boolean() { [native code] })>.

mm.. maybe i do something wrong) or is it another way to write test for value in that case:

mm ..也许我做错了什么)或者是在这种情况下编写测试值的另一种方法:

if (true or false) - passed, if any other - fail

if(true或false) - 传递,如果有的话 - 失败

4 个解决方案

#1


10  

I think what you need is a custom Matcher something like this:

我认为你需要的是一个自定义Matcher,如下所示:

toBeBoolean : function () {
  return {
    compare : function (actual, expected) {
      return {
        pass : (typeof actual === 'boolean'),
        message : 'Expected ' + actual + ' is not boolean'
      };
    }
  };
}

How to create a Custom Matcher

如何创建自定义匹配器

#2


8  

Also possible with:

也可以:

expect(item).toMatch(/true|false/);

#3


6  

I would expect your code work as expected but obviously it seems like jasmine behaves a little weird in this case.

我希望你的代码按预期工作,但显然在这种情况下茉莉似乎有点奇怪。

I would recommend you the following workaround:

我建议您使用以下解决方法:

expect(typeof item).toEqual('boolean');

#4


1  

For me, the marked solution isn't correct. At least not for the way I expect it to be used. I did it like this:

对我来说,标记的解决方案是不正确的。至少不是因为我期望它被使用的方式。我是这样做的:

jasmine.addMatchers({
    toBeBoolean: function () {
        return {
            compare: function (actual, expected) {
                return {
                    pass: typeof actual === 'boolean',
                    message: 'Expected ' + actual + ' is not boolean'
                };
            }
        };
    }
});

because you are not passing an expected value, just the actual. So the way you would invoke this is:

因为你没有传递预期的值,只是实际的。所以你调用它的方式是:

expect(true|false).toBeBoolean();

#1


10  

I think what you need is a custom Matcher something like this:

我认为你需要的是一个自定义Matcher,如下所示:

toBeBoolean : function () {
  return {
    compare : function (actual, expected) {
      return {
        pass : (typeof actual === 'boolean'),
        message : 'Expected ' + actual + ' is not boolean'
      };
    }
  };
}

How to create a Custom Matcher

如何创建自定义匹配器

#2


8  

Also possible with:

也可以:

expect(item).toMatch(/true|false/);

#3


6  

I would expect your code work as expected but obviously it seems like jasmine behaves a little weird in this case.

我希望你的代码按预期工作,但显然在这种情况下茉莉似乎有点奇怪。

I would recommend you the following workaround:

我建议您使用以下解决方法:

expect(typeof item).toEqual('boolean');

#4


1  

For me, the marked solution isn't correct. At least not for the way I expect it to be used. I did it like this:

对我来说,标记的解决方案是不正确的。至少不是因为我期望它被使用的方式。我是这样做的:

jasmine.addMatchers({
    toBeBoolean: function () {
        return {
            compare: function (actual, expected) {
                return {
                    pass: typeof actual === 'boolean',
                    message: 'Expected ' + actual + ' is not boolean'
                };
            }
        };
    }
});

because you are not passing an expected value, just the actual. So the way you would invoke this is:

因为你没有传递预期的值,只是实际的。所以你调用它的方式是:

expect(true|false).toBeBoolean();