Refactor tests to depend on jasmineUnderTest
This commit is contained in:
@@ -1,59 +1,85 @@
|
|||||||
describe('toHaveSpyInteractions', function() {
|
describe('toHaveSpyInteractions', function () {
|
||||||
it('detects spy interactions', function() {
|
|
||||||
|
it('detects spy interactions', function () {
|
||||||
|
let matcher = jasmineUnderTest.matchers.toHaveSpyInteractions();
|
||||||
let spyObj = jasmineUnderTest.getEnv().createSpyObj('NewClass', ['spyA', 'spyB']);
|
let spyObj = jasmineUnderTest.getEnv().createSpyObj('NewClass', ['spyA', 'spyB']);
|
||||||
|
|
||||||
spyObj.spyA();
|
spyObj.spyA();
|
||||||
|
|
||||||
expect(spyObj).toHaveSpyInteractions();
|
let result = matcher.compare(spyObj);
|
||||||
|
expect(result.pass).toBe(true);
|
||||||
|
expect(result.message).toContain(
|
||||||
|
'Expected spy object spies to have been called'
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('detects multiple spy interactions', function() {
|
it('detects multiple spy interactions', function () {
|
||||||
|
let matcher = jasmineUnderTest.matchers.toHaveSpyInteractions();
|
||||||
let spyObj = jasmineUnderTest.getEnv().createSpyObj('NewClass', ['spyA', 'spyB']);
|
let spyObj = jasmineUnderTest.getEnv().createSpyObj('NewClass', ['spyA', 'spyB']);
|
||||||
|
|
||||||
spyObj.spyA();
|
spyObj.spyA();
|
||||||
spyObj.spyB();
|
spyObj.spyB();
|
||||||
spyObj.spyA();
|
spyObj.spyA();
|
||||||
|
|
||||||
expect(spyObj).toHaveSpyInteractions();
|
let result = matcher.compare(spyObj);
|
||||||
|
expect(result.pass).toBe(true);
|
||||||
|
expect(result.message).toContain(
|
||||||
|
'Expected spy object spies to have been called'
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('detects no spy interactions', function() {
|
it('detects no spy interactions', function () {
|
||||||
|
let matcher = jasmineUnderTest.matchers.toHaveSpyInteractions();
|
||||||
let spyObj = jasmineUnderTest.getEnv().createSpyObj('NewClass', ['spyA', 'spyB']);
|
let spyObj = jasmineUnderTest.getEnv().createSpyObj('NewClass', ['spyA', 'spyB']);
|
||||||
|
|
||||||
expect(spyObj).not.toHaveSpyInteractions();
|
let result = matcher.compare(spyObj);
|
||||||
|
expect(result.pass).toBe(false);
|
||||||
|
expect(result.message).toContain(
|
||||||
|
'Expected spy object spies to have been called'
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('ignores non-observed spy object interactions', function() {
|
it('ignores non-observed spy object interactions', function () {
|
||||||
|
let matcher = jasmineUnderTest.matchers.toHaveSpyInteractions();
|
||||||
let spyObj = jasmineUnderTest.getEnv().createSpyObj('NewClass', ['spyA', 'spyB']);
|
let spyObj = jasmineUnderTest.getEnv().createSpyObj('NewClass', ['spyA', 'spyB']);
|
||||||
|
spyObj.otherMethod = function () { };
|
||||||
|
|
||||||
spyObj.otherMethod();
|
spyObj.otherMethod();
|
||||||
|
|
||||||
expect(spyObj).not.toHaveSpyInteractions();
|
let result = matcher.compare(spyObj);
|
||||||
|
expect(result.pass).toBe(false);
|
||||||
|
expect(result.message).toContain(
|
||||||
|
'Expected spy object spies to have been called'
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
[true, 123, 'string'].forEach(function(testValue) {
|
[true, 123, 'string'].forEach(function (testValue) {
|
||||||
it(`throws error if a non-object (${testValue}) is passed`, function() {
|
it(`throws error if a non-object (${testValue}) is passed`, function () {
|
||||||
expect(function() {
|
let matcher = jasmineUnderTest.matchers.toHaveSpyInteractions();
|
||||||
expect(testValue).toHaveSpyInteractions();
|
|
||||||
|
expect(function () {
|
||||||
|
matcher.compare(testValue);
|
||||||
}).toThrowError(Error, /Expected a spy object, but got/);
|
}).toThrowError(Error, /Expected a spy object, but got/);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws error if arguments are passed', function() {
|
it('throws error if arguments are passed', function () {
|
||||||
|
let matcher = jasmineUnderTest.matchers.toHaveSpyInteractions();
|
||||||
let spyObj = jasmineUnderTest.getEnv().createSpyObj('NewClass', ['spyA', 'spyB']);
|
let spyObj = jasmineUnderTest.getEnv().createSpyObj('NewClass', ['spyA', 'spyB']);
|
||||||
|
|
||||||
expect(function() {
|
expect(function () {
|
||||||
expect(spyObj).toHaveSpyInteractions('an argument');
|
matcher.compare(spyObj, 'an argument');
|
||||||
}).toThrowError(Error, /Does not take arguments/);
|
}).toThrowError(Error, /Does not take arguments/);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws error if spy object has no spies', function() {
|
it('throws error if spy object has no spies', function () {
|
||||||
const spyObj = jasmineUnderTest.getEnv().createSpyObj('NewClass', ['method']);
|
let matcher = jasmineUnderTest.matchers.toHaveSpyInteractions();
|
||||||
|
const spyObj = jasmineUnderTest.getEnv().createSpyObj('NewClass', ['notSpy']);
|
||||||
// Removing spy since spy objects cannot be created without spies.
|
// Removing spy since spy objects cannot be created without spies.
|
||||||
spyObj.method = function() {};
|
spyObj.notSpy = function () { };
|
||||||
|
|
||||||
expect(function() {
|
expect(function () {
|
||||||
expect(spyObj).toHaveSpyInteractions();
|
matcher.compare(spyObj);
|
||||||
}).toThrowError(
|
}).toThrowError(
|
||||||
Error,
|
Error,
|
||||||
/Expected a spy object with spies, but object has no spies/
|
/Expected a spy object with spies, but object has no spies/
|
||||||
|
|||||||
Reference in New Issue
Block a user