私は最近、ジャスミンでユニットテストをJavaScriptで学び始めました。今私はカルマテストランナーを使って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
})
}
Iはジャスミン(JSFiddleにHERE)上でこれらのテストを実行すると、それは通過するが、WebStorm、それが失敗した:
[カード{図:1、色: 'A' のtoString:関数( {{}}、{{1}、{color}}、{toString:function { ){...}}、 カード{figure:1、color: 'D'、toString:function(){...}}]
オブジェクトを含めると予想される[NaN、NaN、NaN、NaN] ({図:1、色: 'A'})。 @テスト/ test.js:10:9 [3]のhttp:// localhostを:9877/context.js:151:7
それにconsole.logから適切な値を出力したが、テストは失敗し、示されるようにオブジェクトはNaNとして扱われます。
さらに、同じオブジェクトを、new
キーワードなしで、リテラルオブジェクト表記で作成すると、すべてのテストが問題なく実行されます。だから、ここでコンストラクタが問題になっているようです。
このような状況の原因は何ですか?
をしかし、私はジャスミン・ノードとのコンソールでそれを実行するときに、なぜテストは合格?リンクされたjsfiddleで? –
jsfiddleあなたはjasmine v.1.3.1を使用していたので、reasonにはjasmineのバージョンとcontains関数の差分があります。 – num8er
さて、私は理解しています、男に感謝! –