onkami
onkami

Reputation: 9411

Jasmine Testing angular directive with [^form] dependency

I am trying to test a directive, as explained e.g. here http://angular-tips.com/blog/2014/06/introduction-to-unit-test-directives/.

However, in my directive I use form, so I have this in my directive declaration object:

        return {
            link: link,
            restrict: 'E',
            require: ['^form'], // <- I have this !!
            scope: { //...
            },
            controller: function ($scope) {
            //...
            }
        };

as such, when I execute the usual prerequisite for my Jasmine test

element = '<mydirective/>';
element = $compile(element)(scope);

I am getting following dependency problem when trying to run karma / Jasmine test:

Error: [$compile:ctreq] Controller 'form', required by directive 'mydirective', can't be found! http://errors.angularjs.org/1.4.2/$compile/ctreq?p0=form&p1=mydirective

How this can be fixed?

Upvotes: 5

Views: 1021

Answers (1)

JB Nizet
JB Nizet

Reputation: 691765

Use

'<form><mydirective></mydirective></form>'

, and use element.find('mydirective') to find the actual directive element.

Upvotes: 6

Related Questions