Implement withContext for async expectations too
This commit is contained in:
@@ -2339,6 +2339,12 @@ getJasmineRequireObj().AsyncExpectation = function(j$) {
|
|||||||
var expect = new AsyncExpectation(options);
|
var expect = new AsyncExpectation(options);
|
||||||
expect.not = expect.addFilter(negatingFilter);
|
expect.not = expect.addFilter(negatingFilter);
|
||||||
|
|
||||||
|
expect.withContext = function(message) {
|
||||||
|
var result = this.addFilter(new ContextAddingFilter(message));
|
||||||
|
result.not = result.addFilter(negatingFilter);
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
return expect;
|
return expect;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -2370,6 +2376,14 @@ getJasmineRequireObj().AsyncExpectation = function(j$) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function ContextAddingFilter(message) {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
ContextAddingFilter.prototype.modifyFailureMessage = function(msg) {
|
||||||
|
return this.message + ': ' + msg;
|
||||||
|
};
|
||||||
|
|
||||||
return AsyncExpectation;
|
return AsyncExpectation;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -390,6 +390,137 @@ describe('AsyncExpectation', function() {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#withContext', function() {
|
||||||
|
it("prepends the context to the generated failure message", function() {
|
||||||
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
|
spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved')
|
||||||
|
.and.returnValue(Promise.resolve({pass: false}));
|
||||||
|
|
||||||
|
var util = {
|
||||||
|
buildFailureMessage: function() { return 'failure message'; }
|
||||||
|
},
|
||||||
|
addExpectationResult = jasmine.createSpy('addExpectationResult'),
|
||||||
|
actual = dummyPromise(),
|
||||||
|
expectation = jasmineUnderTest.AsyncExpectation.factory({
|
||||||
|
actual: actual,
|
||||||
|
addExpectationResult: addExpectationResult,
|
||||||
|
util: util
|
||||||
|
});
|
||||||
|
|
||||||
|
return expectation.withContext('Some context').toBeResolved()
|
||||||
|
.then(
|
||||||
|
function() {
|
||||||
|
expect(addExpectationResult).toHaveBeenCalledWith(false,
|
||||||
|
jasmine.objectContaining({
|
||||||
|
message: 'Some context: failure message'
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("prepends the context to a custom failure message", function() {
|
||||||
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
|
spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved')
|
||||||
|
.and.returnValue(Promise.resolve({pass: false, message: 'msg'}));
|
||||||
|
|
||||||
|
var util = {
|
||||||
|
buildFailureMessage: function() { return 'failure message'; }
|
||||||
|
},
|
||||||
|
addExpectationResult = jasmine.createSpy('addExpectationResult'),
|
||||||
|
actual = dummyPromise(),
|
||||||
|
expectation = jasmineUnderTest.AsyncExpectation.factory({
|
||||||
|
actual: actual,
|
||||||
|
addExpectationResult: addExpectationResult,
|
||||||
|
util: util
|
||||||
|
});
|
||||||
|
|
||||||
|
return expectation.withContext('Some context').toBeResolved()
|
||||||
|
.then(
|
||||||
|
function() {
|
||||||
|
expect(addExpectationResult).toHaveBeenCalledWith(false,
|
||||||
|
jasmine.objectContaining({
|
||||||
|
message: 'Some context: msg'
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("prepends the context to a custom failure message from a function", function() {
|
||||||
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
|
spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved')
|
||||||
|
.and.returnValue(Promise.resolve({pass: false, message: function() { return 'msg'; } }));
|
||||||
|
|
||||||
|
var util = {
|
||||||
|
buildFailureMessage: function() { return 'failure message'; }
|
||||||
|
},
|
||||||
|
addExpectationResult = jasmine.createSpy('addExpectationResult'),
|
||||||
|
actual = dummyPromise(),
|
||||||
|
expectation = jasmineUnderTest.AsyncExpectation.factory({
|
||||||
|
actual: actual,
|
||||||
|
addExpectationResult: addExpectationResult,
|
||||||
|
util: util
|
||||||
|
});
|
||||||
|
|
||||||
|
return expectation.withContext('Some context').toBeResolved()
|
||||||
|
.then(
|
||||||
|
function() {
|
||||||
|
expect(addExpectationResult).toHaveBeenCalledWith(false,
|
||||||
|
jasmine.objectContaining({
|
||||||
|
message: 'Some context: msg'
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("works with #not", function() {
|
||||||
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
|
spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved')
|
||||||
|
.and.returnValue(Promise.resolve({pass: true}));
|
||||||
|
|
||||||
|
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
|
||||||
|
actual = dummyPromise(),
|
||||||
|
expectation = jasmineUnderTest.AsyncExpectation.factory({
|
||||||
|
actual: actual,
|
||||||
|
addExpectationResult: addExpectationResult,
|
||||||
|
util: jasmineUnderTest.matchersUtil
|
||||||
|
});
|
||||||
|
|
||||||
|
return expectation.withContext('Some context').not.toBeResolved()
|
||||||
|
.then(
|
||||||
|
function() {
|
||||||
|
expect(addExpectationResult).toHaveBeenCalledWith(false,
|
||||||
|
jasmine.objectContaining({
|
||||||
|
message: 'Some context: Expected a promise not to be resolved.'
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("works with #not and a custom message", function() {
|
||||||
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
|
spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved')
|
||||||
|
.and.returnValue(Promise.resolve({pass: true, message: 'msg'}));
|
||||||
|
|
||||||
|
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
|
||||||
|
actual = dummyPromise(),
|
||||||
|
expectation = jasmineUnderTest.AsyncExpectation.factory({
|
||||||
|
actual: actual,
|
||||||
|
addExpectationResult: addExpectationResult,
|
||||||
|
util: jasmineUnderTest.matchersUtil
|
||||||
|
});
|
||||||
|
|
||||||
|
return expectation.withContext('Some context').not.toBeResolved()
|
||||||
|
.then(
|
||||||
|
function() {
|
||||||
|
expect(addExpectationResult).toHaveBeenCalledWith(false,
|
||||||
|
jasmine.objectContaining({
|
||||||
|
message: 'Some context: msg'
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
function dummyPromise() {
|
function dummyPromise() {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -193,6 +193,12 @@ getJasmineRequireObj().AsyncExpectation = function(j$) {
|
|||||||
var expect = new AsyncExpectation(options);
|
var expect = new AsyncExpectation(options);
|
||||||
expect.not = expect.addFilter(negatingFilter);
|
expect.not = expect.addFilter(negatingFilter);
|
||||||
|
|
||||||
|
expect.withContext = function(message) {
|
||||||
|
var result = this.addFilter(new ContextAddingFilter(message));
|
||||||
|
result.not = result.addFilter(negatingFilter);
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
return expect;
|
return expect;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -224,5 +230,13 @@ getJasmineRequireObj().AsyncExpectation = function(j$) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function ContextAddingFilter(message) {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
ContextAddingFilter.prototype.modifyFailureMessage = function(msg) {
|
||||||
|
return this.message + ': ' + msg;
|
||||||
|
};
|
||||||
|
|
||||||
return AsyncExpectation;
|
return AsyncExpectation;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user