There are three options for using ngSanitize in tests:
- enter the service in your test
- drown out a method call on ngSanitize
- mock whole ngSanitize service
The option you choose really depends on using ngSanitize in your working code (and not in the test code).
Whatever you choose, you need to make the service available in your test, there is no need for $provider (this covers option 1, and there is no need to do more than this if you just want to make it available to your filter):
beforeEach(module('ngSanitize')); beforeEach(inject(function(_ngSanitize_) {
Also, make sure all js files are uploaded and uploaded by karma. You can define this in karma.conf.js by adding them to the files: property.
2. Configure the service method
I like stubs and they are very useful when writing tests. Your tests should only check one, and in your case, a filter. The blanks give you more control over your trials and allow you to isolate the test item.
Typically, filters, controllers, require a lot of other things (services or factories such as $ http or ngSanitize).
Assuming your filter uses ngSanitize $sanitize to sanitize some html, you can drown out this method to return the sanitized html you defined to test your expectations:
// in a beforeEach spyOn(mockNgSanitize, "$sanitize").and.returnValue('<some>sanitized<html>'); mockNgSanitized.$sanitize(yourDirtyHtml);
See jasmine docs for more details .
You may have to play spying on the right service, but this should work fine.
3. Trick the whole service
I don’t think you want to go with this option, because it will make you go crazy, figuring out what mockery plus mocks is needed, can create unrealistic expectations, and is also not particularly useful for your use case. If you really want to go, then something like below goes in the right direction ( see jasmine docs again)
beforeEach(function() { mockNgSanitize = ('ngSanitize', ['linky', '$sanitize', '$sanitizeProvider']; }); it('mocks the ngSanitize service', function() { expect(mockNgSanitize.linky).toBeDefined(); });
NB: throughout the code above, make sure that you continue to declare any variables at the top of your description block.