Files
jasmine/spec/core/matchers/toHaveBeenCalledSpec.js
Steve Gravrock 434575f49d Use one declaration per statement
The old style of merging all of a function's variable declarations into
a single statement made some sense back in the days of var, but there's
no reason to keep doing it now that we use const and let.
2026-03-11 06:30:46 -07:00

66 lines
2.0 KiB
JavaScript

describe('toHaveBeenCalled', function() {
it('passes when the actual was called, with a custom .not fail message', function() {
const matcher = privateUnderTest.matchers.toHaveBeenCalled();
const calledSpy = new privateUnderTest.Spy('called-spy');
calledSpy();
const result = matcher.compare(calledSpy);
expect(result.pass).toBe(true);
expect(result.message).toEqual(
'Expected spy called-spy not to have been called.'
);
});
it('fails when the actual was not called', function() {
const matcher = privateUnderTest.matchers.toHaveBeenCalled();
const uncalledSpy = new privateUnderTest.Spy('uncalled spy');
const result = matcher.compare(uncalledSpy);
expect(result.pass).toBe(false);
});
it('throws an exception when the actual is not a spy', function() {
const matcher = privateUnderTest.matchers.toHaveBeenCalled({
pp: privateUnderTest.makePrettyPrinter()
});
const fn = function() {};
expect(function() {
matcher.compare(fn);
}).toThrowError(Error, /Expected a spy, but got Function./);
});
it('throws an exception when invoked with any arguments', function() {
const matcher = privateUnderTest.matchers.toHaveBeenCalled();
const spy = new privateUnderTest.Spy('sample spy');
expect(function() {
matcher.compare(spy, 'foo');
}).toThrowError(Error, /Does not take arguments, use toHaveBeenCalledWith/);
});
it('has a custom message on failure', function() {
const matcher = privateUnderTest.matchers.toHaveBeenCalled();
const spy = new privateUnderTest.Spy('sample-spy');
const result = matcher.compare(spy);
expect(result.message).toEqual(
'Expected spy sample-spy to have been called.'
);
});
it('set the correct calls as verified when passing', function() {
const matcher = privateUnderTest.matchers.toHaveBeenCalled();
const spy = new privateUnderTest.Spy('sample-spy');
spy();
matcher.compare(spy);
expect(spy.calls.count()).toBe(1);
expect(spy.calls.unverifiedCount()).toBe(0);
});
});