Here is how you can unit test a function defined in a link function in a directive with isolate scope:
http://plnkr.co/edit/QOpTP2KZ1rCLLtb5VQme?p=preview Basically, get a reference to the isolate scope by calling the isolateScope() method on your compiled directive. From that, you can call your function or set up spies. Hope this helps! On Saturday, March 29, 2014 1:20:31 PM UTC-4, Nikita Tovstoles wrote: > > Sorry if I am being thick them. Looking at the example below how would you > invoke doStuff() - and only fiXture() - in a unit test? > On Mar 29, 2014 9:00 AM, "John Walker" <[email protected] <javascript:>> > wrote: > >> I think we are talking about the same thing. scope.doStuff() could be >> tested from within a link function using Jasmine and the spy test methods. >> >> >> On Sat, Mar 29, 2014 at 8:18 AM, Nikita Tovstoles >> <[email protected]<javascript:> >> > wrote: >> >>> No thanks, John. I really was just curious about unit-testing directive >>> methods like doStuff() as I described below. Although I >>> understand Matjaž's point re: testing those implicitly by testing entire >>> directive behavior, in more complex directives, it is often natural to >>> split functionality into several smaller units and it'd be nice to be able >>> to unit-test those without having to move them out of link() into a >>> directive controller. >>> >>> -nikita >>> >>> >>> >>> On Fri, Mar 28, 2014 at 3:07 PM, John Walker >>> <[email protected]<javascript:> >>> > wrote: >>> >>>> You could have functions within an isolated scope that are passed in >>>> via the '&' or just local functions directives would use. They just are >>>> not >>>> tied to a controller. Is that something you need help with? >>>> >>>> >>>> On Friday, March 28, 2014 9:55:12 AM UTC-6, Nikita Tovstoles wrote: >>>> >>>>> Ok thanks >>>>> On Mar 28, 2014 8:48 AM, "Matjaž Lipuš" <[email protected]> wrote: >>>>> >>>>>> on isolate scope you don't assert scope values. but directive >>>>>> behaviour. >>>>>> so in unit test you trigger some event or sth. that calls doStruff >>>>>> and assert expected result of doStruff. >>>>>> >>>>>> On Friday, March 28, 2014 4:25:01 PM UTC+1, Nikita Tovstoles wrote: >>>>>>> >>>>>>> Sorry, John, I am not sure the above quite does it. This may or may >>>>>>> not matter, but I was thinking of a directive with isolate scope, ie: >>>>>>> >>>>>>> app.directive('foobar', function(){ >>>>>>> return { >>>>>>> restrict: 'E', >>>>>>> scope: {}, >>>>>>> link: function(scope){ >>>>>>> scope.doStuff = function(){}; >>>>>>> } >>>>>>> }; >>>>>>> }); >>>>>>> >>>>>>> how would you unit-test doStuff()? in your example, you're asserting >>>>>>> on the same scope as was used to compile directive, but in this case >>>>>>> scope >>>>>>> is a child of that scope - how does one get at it? >>>>>>> >>>>>>> -nikita >>>>>>> >>>>>>> >>>>>>> On Fri, Mar 28, 2014 at 7:33 AM, John Walker >>>>>>> <[email protected]>wrote: >>>>>>> >>>>>>>> Sorry, i just realized you were asking about scope functions, not >>>>>>>>>>> scope properties. If that doesn't give you what you need, let me >>>>>>>>>>> know and >>>>>>>>>>> i'll throw a function in the unit test. >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> -- >>>>>>>> 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]. >>>>>>>> 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. >>>>>>>> >>>>>>> >>>>>>> -- >>>>>> 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]. >>>>>> 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. >>>>>> >>>>> -- >>>> 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 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 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.
