爲了使這樣的工作:
expect(3).toBeIn([6,5,3,2]);
茉莉花有一個功能叫做匹配器:
這是一個關於如何申報他們的例子。我宣佈在最後,你要尋找的方法:
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
}
}
};
}
重要茉莉2.0。我不得不使用jasmine.addMatchers({茉莉花specrunner.html但是當我噶配置它,我曾與更換茉莉花這像this.addMatchers({因爲噶使用茉莉花的早期版本
嘗試檢出https://github.com/pivotal/jasmine/wiki/Matchers。 –