注:このソリューションには、Jasminev2.0より前のバージョンの構文が含まれています。現在のカスタムマッチャーの詳細については、https ://jasmine.github.io/2.0/custom_matcher.htmlを参照してください。
Matchers.jsは、単一の「結果修飾子」でのみ機能します- not
:
core / Spec.js:
jasmine.Spec.prototype.expect = function(actual) {
var positive = new (this.getMatchersClass_())(this.env, actual, this);
positive.not = new (this.getMatchersClass_())(this.env, actual, this, true);
return positive;
core / Matchers.js:
jasmine.Matchers = function(env, actual, spec, opt_isNot) {
...
this.isNot = opt_isNot || false;
}
...
jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) {
return function() {
...
if (this.isNot) {
result = !result;
}
}
}
したがって、実際に独自のマッチャーを作成する必要があるようです(正しい場合はabefore
またはit
bloc内からthis
)。例えば:
this.addMatchers({
toBeAnyOf: function(expecteds) {
var result = false;
for (var i = 0, l = expecteds.length; i < l; i++) {
if (this.actual === expecteds[i]) {
result = true;
break;
}
}
return result;
}
});