m-e-conroy / angular-dialog-service

A complete AngularJS service with controllers and templates for generating application modals and dialogs for use with Angular-UI-Bootstrap and Twitter Bootstrap
MIT License
618 stars 228 forks source link

Mocking dialog in jasmine tests #48

Open mbcooper opened 10 years ago

mbcooper commented 10 years ago

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

m-e-conroy commented 10 years ago

I'll look into this.

kegsay commented 9 years ago

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.

samudurand commented 9 years ago

Excellent solution @Kegsay , it worked perfectly for me.

dciccale commented 9 years ago

Thanks @Kegsay that worked perfectly