使用Jasmine的Jasmine和Karma会得到不同的结果

时间:2016-11-11 22:00:27

标签: javascript jasmine karma-runner webstorm

我最近开始用JavaScript学习Jasmine的单元测试。现在我正在使用Karma测试运行器在WebStorm工作。当我通过控制台测试Jasmine时,以及通过WebStorm中的Karma / Jasmine测试时,一些结果有所不同。

例如,当我创建具有简化结构的项目时,如下所示:

.
├── _js
|   └── script.js
├── _test
|   └── test.js
├── karma.conf.js
└── index.html

的script.js

function Card(figure, color) {
    "use strict";

    var that = this;
    this.figure = figure;
    this.color = color;
    this.toString = function () {
        return that.col + that.fig;
    };
}

test.js

describe("The validation of name", function () {

    it("should return true if object is properly initialized", function () {
        var a = new Card(1,"A");
        expect(a.figure === 1)
    });

    it("should return true if array contain card", function () {
        var a = [new Card(1,"A"),new Card(1,"B"),new Card(1,"C"),new Card(1,"D")];
        console.log(a);
        expect(a).toContain({figure: 1, color: "A"});
    });
})

karma.conf.js

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine'],
    files: ['js/*.js', 'test/*.js'],
    exclude: [],
    preprocessors: {},
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: false,
    browsers: ['Firefox'],
    singleRun: false,
    concurrency: Infinity
  })
}

当我在Jasmine上运行这些测试时(HERE在JSFiddle上),它会通过,但在WebStorm中它会失败,但是:

  

[卡片{图:1,颜色:' A',toString:function(){...}},   卡片{图:1,颜色:' B',toString:function(){...}},   卡片{图:1,颜色:' C',toString:function(){...}},   卡{图:1,颜色:' D',toString:function(){...}}]

     

预期[NaN,NaN,NaN,NaN]包含Object({figure:1,color:   ' A' })。 @测试/ test.js:10:9     [3]的http://本地主机:9877 / context.js:151:7

它从console.log打印正确的值,但测试失败,如上所示,对象被视为NaN。

更重要的是,如果我使用文字对象表示法创建没有new关键字的相同对象,则所有测试都没有问题。所以似乎构造函数在这里是一个问题。

这种情况可能是什么原因?

1 个答案:

答案 0 :(得分:1)

你的考试不会通过。

因为toContain检查所有对象是否包含已定义的属性。

所以你要编写你的自定义匹配器。

检查此示例:

// source code
function Card(figure, color) {
  "use strict";

  var that = this;
  this.figure = figure;
  this.color = color;
  this.toString = function () {
    return that.color + that.figure;
  };
}

var customMatchers = {
  hasObjectInArrayThatContains : function(expected){
    var arrayOfObjects = this.actual;
    // iterating array of objects and finding at least
    // one cituation where it passes test
    for(var i in arrayOfObjects) {
      var failures = 0;
      for(var key in expected) {
        if(arrayOfObjects[i][key] != expected[key]) {
          failures++;
        }
      }
      
      if(failures == 0) {
        return true;
      }
    }
    
    return false;
  } 
};

describe("The validation of name", function () {
  
  beforeEach(function(){
    this.addMatchers(customMatchers); // attaching our custom matchers
  });
  
  it("should return true if object is properly initialized", function () {
    var a = new Card(1,"A");
    expect(a.figure === 1)
  });

  it("should return true if array contain card", function () {
    var a = [new Card(1,"A"),new Card(1,"B"),new Card(1,"C"),new Card(1,"D")];
    expect(a).hasObjectInArrayThatContains({figure: 1, color: "A"}); // using our custom method
  });
});


// load jasmine htmlReporter
(function() {
  var env = jasmine.getEnv();
  env.addReporter(new jasmine.HtmlReporter());
  env.execute();
}());
<link rel="stylesheet" href="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css">
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>

失败的例子:

请参阅:expect(a).hasObjectInArrayThatContains({figure: 1, color: "E"})

// source code
function Card(figure, color) {
  "use strict";

  var that = this;
  this.figure = figure;
  this.color = color;
  this.toString = function () {
    return that.color + that.figure;
  };
}

var customMatchers = {
  hasObjectInArrayThatContains : function(expected){
    var arrayOfObjects = this.actual;
    // iterating array of objects and finding at least
    // one cituation where it passes test
    for(var i in arrayOfObjects) {
      var failures = 0;
      for(var key in expected) {
        if(arrayOfObjects[i][key] != expected[key]) {
          failures++;
        }
      }
      
      if(failures == 0) {
        return true;
      }
    }
    
    return false;
  } 
};

describe("The validation of name", function () {
  
  beforeEach(function(){
    this.addMatchers(customMatchers); // attaching our custom matchers
  });
  
  it("should return true if object is properly initialized", function () {
    var a = new Card(1,"A");
    expect(a.figure === 1)
  });

  it("should return true if array contain card", function () {
    var a = [new Card(1,"A"),new Card(1,"B"),new Card(1,"C"),new Card(1,"D")];
    expect(a).hasObjectInArrayThatContains({figure: 1, color: "E"}); // using our custom method
  });
});


// load jasmine htmlReporter
(function() {
  var env = jasmine.getEnv();
  env.addReporter(new jasmine.HtmlReporter());
  env.execute();
}());
<link rel="stylesheet" href="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css">
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>

工作小提琴:https://jsfiddle.net/3VuGs/396/

还请阅读:testing Your js with Jasmine