From a4f023f5b9da9e7ed993be4302889d0313c2ee9b Mon Sep 17 00:00:00 2001 From: Vasu Bhog Date: Mon, 12 Sep 2022 14:06:59 -0700 Subject: [PATCH] Update README.md (#20592) * Update README.md added event mocking tip --- test/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/README.md b/test/README.md index 0c8b59f686..65f0ebc8f9 100644 --- a/test/README.md +++ b/test/README.md @@ -63,6 +63,24 @@ The same principle can be applied to the vscode/azdata APIs. The object in this **IMPORTANT** When using Sinon make sure to call `sinon.restore()` after every test run (using `afterEach` typically) to ensure that the stub doesn't affect other tests. +#### Mocking Events through TypeMoq +Mocking Events can be done by setting up the mocked property to return the the event. In order to have the event callback be registered, the promise that registers the mock element must be setup prior to the event firing. Therefore, once the event is fired it will be triggered as expected. + +**Example of an event mock using TypeMoq:** +``` typescript +// create test event emitter +const onDidChangeSelectionEventEmitter = new vscode.EventEmitter(); +// setup the mock property to return the test event +quickPickMock.setup(q => q.onDidChangeSelection).returns(() => onDidChangeSelectionEventEmitter.event); + +// register the function that is being tested +const promptPromise = connectionUI.promptForConnection(); +// Trigger onDidChangeSelection event to simulate user selecting item +onDidChangeSelectionEventEmitter.fire([item]); +// await the registered promise +await promptPromise; +``` + ### azdata-test package The [@microsoft/azdata-test](https://www.npmjs.com/package/@microsoft/azdata-test) package contains a number of things that may be helpful to extension tests. These include stubs, mocks and general helper functions that many extensions may need to use - such as common patterns for mocking out parts of the extension API.