नोट: इस समाधान में जैस्मीन v2.0 से पहले संस्करणों के लिए वाक्यविन्यास शामिल है। कस्टम matchers अधिक जानकारी के लिए अब, देखें: https://jasmine.github.io/2.0/custom_matcher.html
Matchers.js एक 'परिणाम संशोधक' के साथ काम करता केवल - not
:
कोर/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;
कोर/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;
}
}
}
तो ऐसा लगता है कि आपको वास्तव में this
के लिए before
या it
ब्लॉक के भीतर अपना खुद का मैचर लिखना होगा। उदाहरण के लिए:
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;
}
});
मैंने प्रलेखन की जांच की और मैंने कुछ कोड की कोशिश की, लेकिन मुझे लगता है कि हम "आसानी से" क्षमा नहीं कर सकते हैं। लेकिन जो मैंने पाया है उसके अनुसार, आप अपना खुद का "चेन करने योग्य" मैचर बना सकते हैं। – Magus