0

I want to write a test for the directive.

In App

requiresDependencies = ["someModule1","someModule2"];
var app = angular.module('app', requiresDependencies);

In test

describe("Logon Hours Editor", function ()
{
    var compile, rootScope;
    var element;

    beforeEach(module('app'));

    beforeEach(inject(['$compile', '$rootScope', function ($compile, $rootScope)
    {
        compile = $compile;
        rootScope = $rootScope;
        element = $compile('some html')($rootScope);
    } ]
    )); ....

My directive is relative to the main unit, and I do not want to connect other test modules (someModule1,someModule2) described in "requiresDependencies" ,because later their numbers and names can be changed. How do I connect only without his dependencies?

Eduard Gamonal
  • 8,023
  • 5
  • 41
  • 46
Junik
  • 11
  • 3

1 Answers1

0

You have to mock them. For example, you can create a module called mocks with a d with the services or directives you want to mock. You then load it with beforeEach(module('mocks')); and your calls from the test suite to whatever service you have in mocks will be using the dummy implementation.

You can put this mock module in your test folder, like /test/lib/my-mocks.js (next to angular-mocks.js, if you're using angular-seed). Lastly, you include it in karma.conf.js:

files = [
  'app/lib/jquery/jquery-1.9.1.js',
  JASMINE,
  JASMINE_ADAPTER,
  'test/lib/jasmine-jquery.js',
  'app/lib/angular/angular.js',
  ...
  'test/lib/myproject/my-mocks.js',
   ...
];

Another approach is using jasmine spies. For example:

 spyOn($location, 'absUrl').andCallFake(function (p) {
     return 'http://localhost/app/index.html#/this/is/my/route';
 });
Eduard Gamonal
  • 8,023
  • 5
  • 41
  • 46
  • Is it possible to create such mocks without creating additional files? – b1r3k Feb 06 '14 at 12:08
  • I guess you can, provided that you declare them in a file that karma can see (listed in karma.conf.js). in any case, I'd say that mocked modules should be in a separate file because you don't want them in your app and the goal is to avoid repeating yourself. mocked modules are declared just like regular modules, but you can name them like "myapp.mocks.mymodule" for the sake of clarity – Eduard Gamonal Feb 06 '14 at 12:16
  • 1
    @b1r3k maybe this answer will help you http://stackoverflow.com/questions/19680105/how-do-i-provide-re-usable-sample-data-values-to-my-angularjs-jasmine-unit-tes/19682501#19682501 – Eduard Gamonal Feb 06 '14 at 12:19