Throw error if you add a custom matcher or equality outside of a runnable

[#66789174]
This commit is contained in:
Christopher Amavisca and Greg Cobb
2014-03-05 14:37:17 -08:00
parent a2ac5ef3b6
commit a9e0112a9b
3 changed files with 59 additions and 0 deletions

View File

@@ -416,10 +416,16 @@ getJasmineRequireObj().Env = function(j$) {
};
this.addCustomEqualityTester = function(tester) {
if(!currentRunnable()) {
throw new Error('Custom Equalities must be added in a before function or a spec');
}
runnableResources[currentRunnable().id].customEqualityTesters.push(tester);
};
this.addMatchers = function(matchersToAdd) {
if(!currentRunnable()) {
throw new Error('Matchers must be added in a before function or a spec');
}
var customMatchers = runnableResources[currentRunnable().id].customMatchers;
for (var matcherName in matchersToAdd) {
customMatchers[matcherName] = matchersToAdd[matcherName];

View File

@@ -825,4 +825,51 @@ describe("Env integration", function() {
env.execute();
});
it('throws an exception if you try to add a matcher outside of a runnable', function (done) {
var env = new j$.Env(),
obj = {fn: function () {}},
exception;
env.describe("a suite", function () {
try {
env.addMatchers({myMatcher: function(actual,expected){return false;}});
} catch(e) {
exception = e;
}
});
var assertions = function() {
expect(exception.message).toBe('Matchers must be added in a before function or a spec');
done();
};
env.addReporter({jasmineDone: assertions});
env.execute();
});
it('throws an exception if you try to add a custom equality outside of a runnable', function (done) {
var env = new j$.Env(),
obj = {fn: function () {}},
exception;
env.describe("a suite", function () {
try {
env.addCustomEqualityTester(function(first, second) {return true;});
} catch(e) {
exception = e;
}
});
var assertions = function() {
expect(exception.message).toBe('Custom Equalities must be added in a before function or a spec');
done();
};
env.addReporter({jasmineDone: assertions});
env.execute();
});
});

View File

@@ -42,10 +42,16 @@ getJasmineRequireObj().Env = function(j$) {
};
this.addCustomEqualityTester = function(tester) {
if(!currentRunnable()) {
throw new Error('Custom Equalities must be added in a before function or a spec');
}
runnableResources[currentRunnable().id].customEqualityTesters.push(tester);
};
this.addMatchers = function(matchersToAdd) {
if(!currentRunnable()) {
throw new Error('Matchers must be added in a before function or a spec');
}
var customMatchers = runnableResources[currentRunnable().id].customMatchers;
for (var matcherName in matchersToAdd) {
customMatchers[matcherName] = matchersToAdd[matcherName];