我正在尝试使用ngUpgrade
从ng1移动到ng2,并在升级期间保持我的Karma / Jasmine测试通过,但我遇到了一个我无法弄清楚的错误。< / p>
我已将服务(Data
)升级为ng2 @Injectable
,并使用upgradeAdapter.downgradeNg2Provider
将其注入我的ng1应用。它在生产代码中工作正常。
但是当我尝试在我的测试中注入它时,我收到了这个错误:
Error: [$injector:unpr] Unknown provider: ng2.InjectorProvider <- ng2.Injector <- Data
任何人都有一个在ngUpgrade中间对代码进行单元测试的工作示例吗?我的堆栈是基于Webpack的。我尝试跟踪并转换systemjs guide,但没有任何运气。
karma.conf.js :
module.exports = function (config) {
config.set({
// base path used to resolve all patterns
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files/patterns to load in the browser
files: [{ pattern: 'spec.bundle.js', watched: false }],
// files to exclude
exclude: [],
plugins: [
require('karma-phantomjs-launcher'),
require('karma-jasmine'),
require('karma-webpack')
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: { 'spec.bundle.js': ['webpack'] },
webpack: require('./webpack.config'),
webpackServer: {
noInfo: true // prevent console spamming when running in Karma!
},
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable colors in the output
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// toggle whether to watch files and rerun tests upon incurring changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],
// if true, Karma runs tests once and exits
singleRun: false
});
};
spec.bundle.js
import 'angular';
import 'angular-mocks';
import 'es6-shim';
import 'reflect-metadata';
import 'angular2/src/core/di';
import test from 'angular2/testing';
import browser from 'angular2/platform/testing/browser';
test.setBaseTestProviders(browser.TEST_BROWSER_PLATFORM_PROVIDERS,
browser.TEST_BROWSER_APPLICATION_PROVIDERS);
let context = require.context('./src', true, /\.spec\.js/);
context.keys().forEach(context);
答案 0 :(得分:0)
实际上,你不应该使用有角度的模拟&#39; module
和inject
,但UpgradeProvider
。
目前还没有记录,但有些角色2的源代码中有一些线索。
UpgradeAdapter
实例应该是单身。UpgradeAdapter
将您的提供商添加到UpgradeAdapter.addProvider
单身人士。UpgradeAdapter.bootstrap
启动应用,然后获取UpgradeAdapterRef
回调函数的ready
实例。ng1Injector
实例获取UpgradeAdapterRef
属性。get
方法实例化您的服务。以下是解决方案的掠夺者: https://embed.plnkr.co/EauYut/
我们还在Wishtack上发布了关于角度2迁移的博客文章。我们很快将在博客上填写更多关于角度2测试的帖子。
希望有所帮助