2

I am using Jasmine Standalone to test my Angular directories,

SimpleDirective.js

var app = angular.module("myApp", []);

app.controller('SimpleDirectiveController', function($scope) {
    $scope.customer = {
      name: 'Igor',
      address: '123 Somewhere'
    };
});

app.directive('helloWorld', function() {
    return {
        restrict: 'AE',
        replace: true,

        // Isolate scope:
        // separate the scope inside a directive from the scope outside, and then map the outer scope to a directive's inner scope. 
        scope: {
            customerInfo: '=info'
        },

        //templateUrl points to an external html template.
        templateUrl: 'fixture/hello.html'
    };
});

fixture/hello.html,

<div class="customer"><b>Hello</b> {{customerInfo.name}}</div>

SimpleDirectiveSpec.js,

describe("simpleDirective Test ", function(){

    // Boilerplate starts from here...
    var compile, scope, element;

    // Name of the module my directive is in.
    beforeEach(module('myApp'));

    // The external template file referenced by templateUrl.
    beforeEach(module('fixture/hello.html'));

    beforeEach(inject(function($compile,$rootScope) {

        compile = $compile;
        scope = $rootScope;

        element = angular.element('<div data-hello-world info="customer"></div>');
        compile(element)(scope);
        scope.$digest();
    }));
    // ...Boilerplate ends here

    it('renders the customer template', function() {
        var customer = element.find('.customer');
        expect(customer.length).toBe(1);
    });

});

I get this error,

Error: [$injector:modulerr] Failed to instantiate module fixture/hello.html due to: [$injector:nomod] Module 'fixture/hello.html' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

Any ideas What I have missed?

2 Answers 2

3

You should create a module for your template and put your template html into templateCache.

It should look like this:

angular.module("fixture/hello.html", []).run(["$templateCache", function($templateCache) {
  $templateCache.put("fixture/hello.html",
    "<div class=\"customer\"><b>Hello</b> {{customerInfo.name}}</div>");
}]);

If you are testing with Karma, there is an automation karma module for this purpose called ng-html2js.

Sign up to request clarification or add additional context in comments.

5 Comments

Well, it should be loaded in your test browser. So, try putting it to SimpleDirectiveSpec.js for now. If it works, then you should checkout ng-html2js to automatically inject it to your test browser.
And if you are using Grunt, you can use this module: github.com/karlgoldstein/grunt-html2js
Thanks. I put it in SimpleDirectiveSpec.js, then it fails my test of it('renders the customer template', function() { var customer = element.find('.customer'); expect(customer.length).toBe(1); });
Are you using jQuery in this project? If not, jqLite find method is limited to lookups by tag name. Check here: stackoverflow.com/questions/17283697/…
So glad I found this post. I have been struggling trying to load directives from templateUrl for like 6 hours. The ng-html2js preprocessors would not work for me. Thanks for the answer!
1

I assume you are using karma html pre-processor for html template https://github.com/karma-runner/karma-ng-html2js-preprocessor. Make sure that module generated have the path name that matches the module name you are declaring 'fixture/hello.html'.

You can verify the files by opening chrome developer console and check the source tab, the modules would be loaded as js file. Make sure that the name of the module generated match.

1 Comment

I'm not using Karma. I'm on Jasmine Standalone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.