X Tutup
Skip to content

Commit c1a0af5

Browse files
juliemrMatias Niemela
authored andcommitted
feat(test): add withProviders for per test providers
Closes #5128
1 parent c6afea6 commit c1a0af5

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

modules/angular2/src/testing/test_injector.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export class TestInjector {
3636
}
3737

3838
execute(fn: FunctionWithParamTokens): any {
39+
var additionalProviders = fn.additionalProviders();
40+
if (additionalProviders.length > 0) {
41+
this.addProviders(additionalProviders);
42+
}
3943
if (!this._instantiated) {
4044
this.createInjector();
4145
}
@@ -119,6 +123,22 @@ export function inject(tokens: any[], fn: Function): FunctionWithParamTokens {
119123
return new FunctionWithParamTokens(tokens, fn, false);
120124
}
121125

126+
export class InjectSetupWrapper {
127+
constructor(private _providers: () => any) {}
128+
129+
inject(tokens: any[], fn: Function): FunctionWithParamTokens {
130+
return new FunctionWithParamTokens(tokens, fn, false, this._providers);
131+
}
132+
133+
injectAsync(tokens: any[], fn: Function): FunctionWithParamTokens {
134+
return new FunctionWithParamTokens(tokens, fn, true, this._providers);
135+
}
136+
}
137+
138+
export function withProviders(providers: () => any) {
139+
return new InjectSetupWrapper(providers);
140+
}
141+
122142
/**
123143
* Allows injecting dependencies in `beforeEach()` and `it()`. The test must return
124144
* a promise which will resolve when all asynchronous activity is complete.
@@ -141,8 +161,13 @@ export function injectAsync(tokens: any[], fn: Function): FunctionWithParamToken
141161
return new FunctionWithParamTokens(tokens, fn, true);
142162
}
143163

164+
function emptyArray(): Array<any> {
165+
return [];
166+
}
167+
144168
export class FunctionWithParamTokens {
145-
constructor(private _tokens: any[], private _fn: Function, public isAsync: boolean) {}
169+
constructor(private _tokens: any[], private _fn: Function, public isAsync: boolean,
170+
public additionalProviders: () => any = emptyArray) {}
146171

147172
/**
148173
* Returns the value of the executed function.

modules/angular2/test/testing/testing_public_spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
beforeEach,
1111
inject,
1212
injectAsync,
13+
withProviders,
1314
beforeEachProviders,
1415
TestComponentBuilder
1516
} from 'angular2/testing';
@@ -186,6 +187,13 @@ export function main() {
186187
inject([FancyService], (service) => { expect(service.value).toEqual('async value'); }));
187188
});
188189
});
190+
191+
describe('per test providers', () => {
192+
it('should allow per test providers',
193+
withProviders(() => [bind(FancyService).toValue(new FancyService())])
194+
.inject([FancyService],
195+
(service) => { expect(service.value).toEqual('real value'); }));
196+
});
189197
});
190198

191199
describe('errors', () => {

0 commit comments

Comments
 (0)
X Tutup