Slowing modules in karma / jasmine for AngularJS

I am trying to compile some unit tests in Karma / Jasmine for a specific module in my project, destination-filters .

Decleration Module:

 angular.module('destination-filter', ['ngSanitize']); 

My tests will fail if I do not remove ngSanitize as a dependency. As far as I understand, this is because when the module is instantiated, it tries to get this dependency, but because in my spec.js file I did not declare that the module is not working.

Special file:

 describe('Destination Filter Controller', function () { // Set the variables var $controller; var mockNgSanitize; beforeEach(module('destination-filter')); beforeEach(function() { module(function($provide) { $provide.value('ngSanitize', mockNgSanitize); }); }); beforeEach(inject(function (_$controller_) { $controller = _$controller_('DestinationFilterController'); })); it('should expect the controller to not be null', function() { // Check the controller is set expect($controller).not.toBeNull(); }); }); 

When prototyping services or functions, the $provide method turned out to be very useful, but I'm not sure if I use it correctly. I assume that the $provide used this way cannot make fun of whole modules, but rather services?

To clarify ...['ngSantize'])... do I ...['ngSantize'])... from my slowdown module, the tests are created correctly. I get an Error: [$injector:modulerr] destination-filter message Error: [$injector:modulerr] destination-filter

+6
source share
1 answer

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_) { // the underscores are needed mockNgSanitize = _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.

+7
source

Source: https://habr.com/ru/post/1236690/


All Articles