angular-dialog-service icon indicating copy to clipboard operation
angular-dialog-service copied to clipboard

Mocking dialog in jasmine tests

Open mbcooper opened this issue 10 years ago • 4 comments

Has anyone developed a pattern for mocking the dialog use?

They acre a little like promises, but are not, so I can't use a promise in a mock.

Here is my code snippet:

var dialog = this.dialogs.confirm(confirm, message); dialog.result.then(

i want to mock this use .... Any examples would be most appreciated.

--mike

mbcooper avatar Jul 09 '14 19:07 mbcooper

I'll look into this.

m-e-conroy avatar Jul 18 '14 15:07 m-e-conroy

I solved this by wrapping dialogs in a Service, which provides methods which return dialog.result. This service is used to provide access to dialogs. Therefore, in the Jasmine tests, you just need to mock the service when testing dialogs, and can inject your own promises. This is convenient if you're using dialogs all over the place.

An alternative simpler solution though would be to spyOn(dialogs, "confirm").and.returnValue({result: testDefer.promise}); where testDefer is the $q.defer() you're going to be resolving/rejecting in your test, and dialogs is the mock you're injecting.

kegsay avatar Dec 05 '14 09:12 kegsay

Excellent solution @Kegsay , it worked perfectly for me.

samudurand avatar Dec 19 '14 20:12 samudurand

Thanks @Kegsay that worked perfectly

dciccale avatar Feb 18 '15 13:02 dciccale