如何测试Jasmine中的值是"大于或等于"?

Bry*_*son 85 javascript tdd jasmine

我想确认一个值是十进制(或0),因此该数字应大于或等于零小于1.

describe('percent',function(){  

  it('should be a decimal', function() {

    var percent = insights.percent; 
    expect(percent).toBeGreaterThan(0);
    expect(percent).toBeLessThan(1);

  });

});
Run Code Online (Sandbox Code Playgroud)

我如何模仿"> = 0"?

And*_*rew 99

我知道这是一个古老且已经解决的问题,但我注意到错过了一个相当简洁的解决方案.由于大于或等于小于函数的倒数,请尝试:

expect(percent).not.toBeLessThan(0);
Run Code Online (Sandbox Code Playgroud)

在这种方法中,百分比的值可以由异步函数返回,并作为控制流的一部分进行处理.

  • 应该接受这个答案.另外:`expect(2 + 2).not.toBe(5)`,`expect(2 + 2).toBeGreaterThan(0)`,`expect(2 + 2).toBeLessThan(5)` (4认同)

Bry*_*son 66

您只需要先运行比较操作,然后检查它是否真实.

describe('percent',function(){
  it('should be a decimal',function(){

    var percent = insights.percent;

    expect(percent >= 0).toBeTruthy();
    expect(percent).toBeLessThan(1);

  });   
});
Run Code Online (Sandbox Code Playgroud)

  • 这是有效的,但不幸的是,由失败的"> ="测试产生的信息并不是特别富有表现力("预期是虚假的").顺便说一句,测试不需要异步(好吧,只是挑剔;). (7认同)
  • @hashchange使用[jasmine2-custom-message](https://github.com/avrelian/jasmine2-custom-message)等插件,可以自定义错误消息:`since('预期百分比大于或等于零').期望(百分比> = 0).toBeTruthy();` (2认同)

DrM*_*eod 11

当前版本的Jasmine支持toBeGreaterThan和toBeLessThan.

expect(myVariable).toBeGreaterThan(0);
Run Code Online (Sandbox Code Playgroud)


Jon*_*Raa 5

有点扼杀这不是基本功能

您可以像这样添加自定义匹配器:

JasmineExtensions.js

yourGlobal.addExtraMatchers = function () {
    var addMatcher = function (name, func) {
        func.name = name;
        jasmine.matchers[name] = func;
    };

    addMatcher("toBeGreaterThanOrEqualTo", function () {
                   return {
                       compare: function (actual, expected) {
                           return {
                               pass: actual >= expected
                           };
                       }
                   };
               }
    );
};
Run Code Online (Sandbox Code Playgroud)

实际上,您正在为匹配器定义构造函数 - 它是一个返回匹配器对象的函数.

包括在你'开机'之前.基本匹配器在引导时加载.

您的html文件应如下所示:

<!-- jasmine test framework-->
<script type="text/javascript" src="lib/jasmine-2.0.0/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-2.0.0/jasmine-html.js"></script>

<!-- custom matchers -->
<script type="text/javascript" src="Tests/JasmineExtensions.js"></script>
<!-- initialisation-->
<script type="text/javascript" src="lib/jasmine-2.0.0/boot.js"></script>
Run Code Online (Sandbox Code Playgroud)

然后在你的boot.js中添加调用,在jasmine定义之后但在jasmine.getEnv()之前添加匹配器.获取env实际上是一个(略微误导性地命名)设置调用.

匹配器在Env构造函数中调用setupCoreMatchers时进行设置.

/**
 * ## Require &amp; Instantiate
 *
 * Require Jasmine's core files. Specifically, this requires and attaches all of Jasmine's code to the `jasmine` reference.
 */
window.jasmine = jasmineRequire.core(jasmineRequire);
yourGlobal.addExtraMatchers();

/**
 * Since this is being run in a browser and the results should populate to an HTML page, require the HTML-specific Jasmine code, injecting the same reference.
 */
jasmineRequire.html(jasmine);

/**
 * Create the Jasmine environment. This is used to run all specs in a project.
 */
var env = jasmine.getEnv();
Run Code Online (Sandbox Code Playgroud)

它们显示了在示例测试中添加自定义匹配器的另一种方法,但是它的工作方式是在每次使用一次测试之前重新创建匹配器beforeEach.这看起来非常可怕所以我认为我会采用这种方法.


has*_*nge 5

我今天遇到了同样的问题,事实证明,为其添加自定义匹配器并不困难。自定义匹配器的主要优点是当测试失败时它可以返回有意义的消息。

.toBeAtLeast()所以这里是两个匹配器和的代码.toBeAtMost(),以防它对某人有帮助。

beforeEach( function () {

  // When beforeEach is called outside of a `describe` scope, the matchers are
  // available globally. See http://stackoverflow.com/a/11942151/508355

  jasmine.addMatchers( {

    toBeAtLeast: function () {
      return {
        compare: function ( actual, expected ) {
          var result = {};
          result.pass = actual >= expected;
          if ( result.pass ) {
            result.message = "Expected " + actual + " to be less than " + expected;
          } else {
            result.message = "Expected " + actual + " to be at least " + expected;
          }
          return result;
        }
      };
    },

    toBeAtMost: function () {
      return {
        compare: function ( actual, expected ) {
          var result = {};
          result.pass = actual <= expected;
          if ( result.pass ) {
            result.message = "Expected " + actual + " to be greater than " + expected;
          } else {
            result.message = "Expected " + actual + " to be at most " + expected;
          }
          return result;
        }
      };
    }

  } );

} );
Run Code Online (Sandbox Code Playgroud)


小智 5

我迟到了,但是发布它以防万一有人仍然访问此问题以寻找答案,我使用的是Jasmine版本3.0,如@Patrizio Rullo所述,您可以使用toBeGreaterThanOrEqual / toBeLessThanOrEqual

根据发行说明在版本2.5中添加了它-https: //github.com/jasmine/jasmine/blob/master/release_notes/2.5.0.md

例如

expect(percent).toBeGreaterThanOrEqual(1,"This is optional expect failure message");
Run Code Online (Sandbox Code Playgroud)

要么

expect(percent).toBeGreaterThanOrEqual(1);
Run Code Online (Sandbox Code Playgroud)