parent
5bf6a3af15
commit
0319417a1b
@ -6,6 +6,8 @@ import {
|
|||||||
CONST_EXPR,
|
CONST_EXPR,
|
||||||
stringify,
|
stringify,
|
||||||
isArray,
|
isArray,
|
||||||
|
isType,
|
||||||
|
isFunction,
|
||||||
normalizeBool
|
normalizeBool
|
||||||
} from 'angular2/src/core/facade/lang';
|
} from 'angular2/src/core/facade/lang';
|
||||||
import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions';
|
import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions';
|
||||||
@ -344,7 +346,13 @@ export class BindingBuilder {
|
|||||||
* expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
|
* expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
toClass(type: Type): Binding { return new Binding(this.token, {toClass: type}); }
|
toClass(type: Type): Binding {
|
||||||
|
if (!isType(type)) {
|
||||||
|
throw new BaseException(
|
||||||
|
`Trying to create a class binding but "${stringify(type)}" is not a class!`);
|
||||||
|
}
|
||||||
|
return new Binding(this.token, {toClass: type});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Binds a DI token to a value.
|
* Binds a DI token to a value.
|
||||||
@ -416,8 +424,12 @@ export class BindingBuilder {
|
|||||||
* expect(injector.get(String)).toEqual('Value: 3');
|
* expect(injector.get(String)).toEqual('Value: 3');
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
toFactory(factoryFunction: Function, dependencies?: any[]): Binding {
|
toFactory(factory: Function, dependencies?: any[]): Binding {
|
||||||
return new Binding(this.token, {toFactory: factoryFunction, deps: dependencies});
|
if (!isFunction(factory)) {
|
||||||
|
throw new BaseException(
|
||||||
|
`Trying to create a factory binding but "${stringify(factory)}" is not a function!`);
|
||||||
|
}
|
||||||
|
return new Binding(this.token, {toFactory: factory, deps: dependencies});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
31
modules/angular2/test/core/di/binding_spec.ts
Normal file
31
modules/angular2/test/core/di/binding_spec.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import {
|
||||||
|
AsyncTestCompleter,
|
||||||
|
beforeEach,
|
||||||
|
ddescribe,
|
||||||
|
describe,
|
||||||
|
expect,
|
||||||
|
iit,
|
||||||
|
inject,
|
||||||
|
it,
|
||||||
|
xit,
|
||||||
|
} from 'angular2/test_lib';
|
||||||
|
|
||||||
|
import {bind} from 'angular2/core';
|
||||||
|
|
||||||
|
export function main() {
|
||||||
|
describe('binding', () => {
|
||||||
|
|
||||||
|
describe('type errors', () => {
|
||||||
|
|
||||||
|
it('should throw when trying to create a class binding and not passing a class', () => {
|
||||||
|
expect(() => { bind('foo').toClass(<any>0); })
|
||||||
|
.toThrowError('Trying to create a class binding but "0" is not a class!');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw when trying to create a factory binding and not passing a function', () => {
|
||||||
|
expect(() => { bind('foo').toFactory(<any>0); })
|
||||||
|
.toThrowError('Trying to create a factory binding but "0" is not a function!');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user