angular-dialog-service
angular-dialog-service copied to clipboard
Mocking dialog in jasmine tests
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
I'll look into this.
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.
Excellent solution @Kegsay , it worked perfectly for me.
Thanks @Kegsay that worked perfectly