Its pretty easy. Its really just the scope that you $compiled your
directive with in the unit test in the first place. I'll just copy in one
of my tests, if it doesn't make sense i'lll explain it. Sorry its in
coffeescript if that bothers you.
Fyi this directive sets up a watcher on two fields to make sure they match.
describe 'directive:watch', ->
beforeEach module 'jw.watch'
scope = null
element = null
setInputsToMatch = ->
scope.user.password = '1234'
scope.user.confirm = '1234'
scope.$digest()
beforeEach inject ($compile, $rootScope) ->
scope = $rootScope
scope.user = {}
element = angular.element("
<form name=\"form\">
<input name=\"one\" ng-model=\"user.password\" required />
<input name=\"two\" ng-model=\"user.confirm\" jw-watch=\"user.password\"
required />
</form>
")
$compile(element)(scope)
scope.$digest()
it 'should test the default validity state', ->
expect(scope.form.one.$valid).toBeFalsy()
expect(scope.form.two.$valid).toBeFalsy()
it 'should test validity when inputs do not match', ->
scope.user.password = '1234'
scope.user.confirm = '123'
scope.$digest()
expect(scope.form.one.$valid).toBeTruthy()
expect(scope.form.two.$valid).toBeFalsy()
Is this what you are looking for? In my link function i am watching scope
variables, the ng-models from the html.
On Thursday, March 27, 2014 12:40:32 PM UTC-6, Nikita Tovstoles wrote:
>
> PS. I suppose I should have titled the below as 'how do I unit-test scope
> methods declared inside link()?'
>
>
> On Thu, Mar 27, 2014 at 11:34 AM, Nikita Tovstoles
> <[email protected]<javascript:>
> > wrote:
>
>> I find it easier to unit-test controllers vs. link() functions since it's
>> easy to inject $scope into former in beforeTest() and thus subsequently
>> access $scope properties and methods for unit-testing. ie:
>>
>> var scope = {};
>>
>> beforeEach(function(){
>> $controller(MyCtrl, {$scope:scope});
>> })
>>
>> it('test scope.foo(), function(){
>> expect(scope.foo()).toBe();///etc.
>> });
>>
>> Seems like there is not a way to do the same for link() functions -
>> without modifying build process. Thus, no easy way to unit-test
>> scope.foo(), if declared inside a link() function *(right?)*
>>
>> ...which may be OK since I can just declare foo() inside controller. One
>> issue with keeping logic inside directive controller vs link() that I
>> encountered is dependency on injected ngModelCtrl (ie for directives which
>> require latter). *Is there a way to inject that ctrl instance into
>> directive controller (similar to $element)?* I only came up with:
>>
>> link: function(scope,element, attrs, ngModel){
>> scope.$ngModel = ngModel; //for use in directive controller
>> }
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "AngularJS" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/angular/1yiCh41gb28/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> [email protected] <javascript:>.
>> To post to this group, send email to [email protected]<javascript:>
>> .
>> Visit this group at http://groups.google.com/group/angular.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.