Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
RELNOTES: Make Mock.record() accept methods of mock objects as well.
Browse files Browse the repository at this point in the history
This allows `record(mockObj.foo)()`.

PiperOrigin-RevId: 549329494
Change-Id: Ic762148f1e860a6eb4d16617cf4c2bc2b95f4bb2
  • Loading branch information
SLaks authored and copybara-github committed Jul 19, 2023
1 parent b91ebf6 commit 5ca918f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 5 additions & 2 deletions closure/goog/testing/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,13 @@ goog.testing.Mock.STRICT = 0;
*/
goog.testing.Mock.record = function(obj) {
'use strict';
// If the user passes a method of a mock object, grab the object.
const mockObj = obj.$$mockObj ? obj.$$mockObj : obj;
goog.asserts.assert(
obj.$recording_ !== undefined,
mockObj.$recording_ !== undefined,
'%s is not a mock. Did you pass a real object to record()?', obj);
goog.asserts.assert(
obj.$recording_,
mockObj.$recording_,
'Your mock is in replay mode. You can only call record(mock) before mock.$replay()');
return obj;
};
Expand Down Expand Up @@ -353,6 +355,7 @@ goog.testing.Mock.prototype.$initializeFunctions_ = function(objectToMock) {
var prop = enumerableProperties[i];
if (typeof objectToMock[prop] == 'function') {
this[prop] = goog.bind(this.$mockMethod, this, prop);
this[prop].$$mockObj = this; // Save a reference for record().
if (this.$proxy) {
this.$proxy[prop] = goog.bind(this.$mockMethod, this, prop);
}
Expand Down
2 changes: 2 additions & 0 deletions closure/goog/testing/mock_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,11 @@ testSuite({
const mockControl = new MockControl();
const strictMock = mockControl.createStrictMock(WithCustomToString);
Mock.record(strictMock).doSomething();
Mock.record(strictMock.doSomething)();

mockControl.$replayAll();
strictMock.doSomething();
strictMock.doSomething();

mockControl.$verifyAll();
},
Expand Down

0 comments on commit 5ca918f

Please sign in to comment.