@@ -599,8 +599,8 @@ describe('injector', function() {
599
599
expect ( function ( ) {
600
600
createInjector ( [ function ( $provide ) {
601
601
$provide . factory ( 'service' , function ( service ) { } ) ;
602
- return function ( service ) { }
603
- } ] )
602
+ return function ( service ) { } ;
603
+ } ] ) ;
604
604
} ) . toThrowMinErr ( '$injector' , 'cdep' , 'Circular dependency found: service' ) ;
605
605
} ) ;
606
606
@@ -610,37 +610,51 @@ describe('injector', function() {
610
610
createInjector ( [ function ( $provide ) {
611
611
$provide . factory ( 'a' , function ( b ) { } ) ;
612
612
$provide . factory ( 'b' , function ( a ) { } ) ;
613
- return function ( a ) { }
614
- } ] )
613
+ return function ( a ) { } ;
614
+ } ] ) ;
615
615
} ) . toThrowMinErr ( '$injector' , 'cdep' , 'Circular dependency found: b <- a' ) ;
616
616
} ) ;
617
+
617
618
} ) ;
618
619
} ) ;
619
620
620
621
621
622
describe ( 'retrieval' , function ( ) {
622
- var instance ,
623
- $injector ,
624
- $provide ;
623
+ var instance = { name :'angular' } ;
624
+ var Instance = function ( ) { this . name = 'angular' ; } ;
625
625
626
- beforeEach ( function ( ) {
627
- $injector = createInjector ( [ [ '$provide' , function ( provide ) {
628
- ( $provide = provide ) . value ( 'instance' , instance = { name :'angular' } ) ;
626
+ function createInjectorWithValue ( instanceName , instance ) {
627
+ return createInjector ( [ [ '$provide' , function ( provide ) {
628
+ provide . value ( instanceName , instance ) ;
629
+ } ] ] ) ;
630
+ }
631
+ function createInjectorWithFactory ( serviceName , serviceDef ) {
632
+ return createInjector ( [ [ '$provide' , function ( provide ) {
633
+ provide . factory ( serviceName , serviceDef ) ;
629
634
} ] ] ) ;
635
+ }
636
+
637
+
638
+ it ( 'should retrieve by name' , function ( ) {
639
+ var $injector = createInjectorWithValue ( 'instance' , instance ) ;
640
+ var retrievedInstance = $injector . get ( 'instance' ) ;
641
+ expect ( retrievedInstance ) . toBe ( instance ) ;
630
642
} ) ;
631
643
632
644
633
- it ( 'should retrieve by name and cache instance' , function ( ) {
634
- expect ( instance ) . toEqual ( { name : 'angular' } ) ;
645
+ it ( 'should cache instance' , function ( ) {
646
+ var $injector = createInjectorWithFactory ( 'instance' , function ( ) { return new Instance ( ) ; } ) ;
647
+ var instance = $injector . get ( 'instance' ) ;
635
648
expect ( $injector . get ( 'instance' ) ) . toBe ( instance ) ;
636
649
expect ( $injector . get ( 'instance' ) ) . toBe ( instance ) ;
637
650
} ) ;
638
651
639
652
640
653
it ( 'should call functions and infer arguments' , function ( ) {
641
- expect ( $injector . invoke ( function ( instance ) { return instance ; } ) ) . toBe ( instance ) ;
654
+ var $injector = createInjectorWithValue ( ' instance' , instance ) ;
642
655
expect ( $injector . invoke ( function ( instance ) { return instance ; } ) ) . toBe ( instance ) ;
643
656
} ) ;
657
+
644
658
} ) ;
645
659
646
660
0 commit comments