With providers split into bundles, the test injector is now able to
use providers for a given bundle. Suggested provider lists for tests are
available in `angular2/platform/testing/<platform>`.
Change the providers for a test suite using `setBaseTestProviders`. This
should be done once at the start of the test suite, before any test cases
run.
BREAKING CHANGE: Tests are now required to use `setBaseTestProviders`
to set up. Assuming your tests are run on a browser, setup would change
as follows.
Before:
```js
// Somewhere in test setup
import {BrowserDomAdapter} from 'angular2/src/platform/browser/browser_adapter';
BrowserDomAdapter.makeCurrent
```
After:
```js
// Somewhere in the test setup
import {setBaseTestProviders} from 'angular2/testing';
import {
  TEST_BROWSER_PLATFORM_PROVIDERS,
  TEST_BROWSER_APPLICATION_PROVIDERS
} from 'angular2/platform/testing/browser';
setBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS,
                     TEST_BROWSER_APPLICATION_PROVIDERS);
```
Closes #5351, Closes #5585
Closes #5975
		
	
			
		
			
				
	
	
		
			122 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			122 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
library angular2_testing.angular2_testing;
 | 
						|
 | 
						|
import 'package:test/test.dart';
 | 
						|
 | 
						|
import 'package:angular2/angular2.dart';
 | 
						|
import 'package:angular2/src/core/di/metadata.dart' show InjectMetadata;
 | 
						|
import 'package:angular2/src/core/di/exceptions.dart' show NoAnnotationError;
 | 
						|
import 'package:angular2/src/core/reflection/reflection.dart';
 | 
						|
import 'package:angular2/src/core/reflection/reflection_capabilities.dart';
 | 
						|
import 'package:angular2/src/testing/test_injector.dart';
 | 
						|
export 'package:angular2/src/testing/test_component_builder.dart';
 | 
						|
export 'package:angular2/src/testing/test_injector.dart' show inject;
 | 
						|
import 'package:angular2/platform/testing/browser.dart';
 | 
						|
 | 
						|
/// One time initialization that must be done for Angular2 component
 | 
						|
/// tests. Call before any test methods.
 | 
						|
///
 | 
						|
/// Example:
 | 
						|
///
 | 
						|
/// ```
 | 
						|
/// main() {
 | 
						|
///   initAngularTests();
 | 
						|
///   group(...);
 | 
						|
/// }
 | 
						|
/// ```
 | 
						|
void initAngularTests() {
 | 
						|
  reflector.reflectionCapabilities = new ReflectionCapabilities();
 | 
						|
  setBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS);
 | 
						|
}
 | 
						|
 | 
						|
void _addTestInjectorTearDown() {
 | 
						|
  // Multiple resets are harmless.
 | 
						|
  tearDown(() {
 | 
						|
    _testInjector.reset();
 | 
						|
  });
 | 
						|
}
 | 
						|
 | 
						|
/// Allows overriding default bindings defined in test_injector.dart.
 | 
						|
///
 | 
						|
/// The given function must return a list of DI providers.
 | 
						|
///
 | 
						|
/// Example:
 | 
						|
///
 | 
						|
/// ```
 | 
						|
/// setUpProviders(() => [
 | 
						|
///   provide(Compiler, useClass: MockCompiler),
 | 
						|
///   provide(SomeToken, useValue: myValue),
 | 
						|
/// ]);
 | 
						|
/// ```
 | 
						|
void setUpProviders(Iterable<Provider> providerFactory()) {
 | 
						|
  setUp(() {
 | 
						|
    try {
 | 
						|
      _testInjector.addProviders(providerFactory());
 | 
						|
    } catch (e) {
 | 
						|
      throw 'setUpProviders was called after the injector had '
 | 
						|
          'been used in a setUp or test block. This invalidates the '
 | 
						|
          'test injector';
 | 
						|
    }
 | 
						|
  });
 | 
						|
 | 
						|
  _addTestInjectorTearDown();
 | 
						|
}
 | 
						|
 | 
						|
dynamic _runInjectableFunction(Function fn) {
 | 
						|
  var params = reflector.parameters(fn);
 | 
						|
  List<dynamic> tokens = <dynamic>[];
 | 
						|
  for (var param in params) {
 | 
						|
    var token = null;
 | 
						|
    for (var paramMetadata in param) {
 | 
						|
      if (paramMetadata is Type) {
 | 
						|
        token = paramMetadata;
 | 
						|
      } else if (paramMetadata is InjectMetadata) {
 | 
						|
        token = paramMetadata.token;
 | 
						|
      }
 | 
						|
    }
 | 
						|
    if (token == null) {
 | 
						|
      throw new NoAnnotationError(fn, params);
 | 
						|
    }
 | 
						|
    tokens.add(token);
 | 
						|
  }
 | 
						|
 | 
						|
  var injectFn = new FunctionWithParamTokens(tokens, fn, false);
 | 
						|
  return _testInjector.execute(injectFn);
 | 
						|
}
 | 
						|
 | 
						|
/// Use the test injector to get bindings and run a function.
 | 
						|
///
 | 
						|
/// Example:
 | 
						|
///
 | 
						|
/// ```
 | 
						|
/// ngSetUp((SomeToken token) {
 | 
						|
///   token.init();
 | 
						|
/// });
 | 
						|
/// ```
 | 
						|
void ngSetUp(Function fn) {
 | 
						|
  setUp(() async {
 | 
						|
    await _runInjectableFunction(fn);
 | 
						|
  });
 | 
						|
 | 
						|
  _addTestInjectorTearDown();
 | 
						|
}
 | 
						|
 | 
						|
/// Add a test which can use the test injector.
 | 
						|
///
 | 
						|
/// Example:
 | 
						|
///
 | 
						|
/// ```
 | 
						|
/// ngTest('description', (SomeToken token) {
 | 
						|
///   expect(token, equals('expected'));
 | 
						|
/// });
 | 
						|
/// ```
 | 
						|
void ngTest(String description, Function fn,
 | 
						|
    {String testOn, Timeout timeout, skip, Map<String, dynamic> onPlatform}) {
 | 
						|
  test(description, () async {
 | 
						|
    await _runInjectableFunction(fn);
 | 
						|
  }, testOn: testOn, timeout: timeout, skip: skip, onPlatform: onPlatform);
 | 
						|
 | 
						|
  _addTestInjectorTearDown();
 | 
						|
}
 | 
						|
 | 
						|
final TestInjector _testInjector = getTestInjector();
 |