Angular2 单元测试组件获得 "No Provider for DirectiveResolver"
Angular2 Unit Test Component getting "No Provider for DirectiveResolver"
我收到以下错误:
Error: No provider for DirectiveResolver!
Error: DI Exception
at NoProviderError.BaseException [as constructor] (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/facade/exceptions.js:19:23)
at NoProviderError.AbstractProviderError [as constructor] (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/core/di/reflective_exceptions.js:41:16)
at new NoProviderError (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/core/di/reflective_exceptions.js:77:16)
at ReflectiveInjector_._throwOrNull (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/core/di/reflective_injector.js:779:19)
at ReflectiveInjector_._getByKeyDefault (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/core/di/reflective_injector.js:807:25)
at ReflectiveInjector_._getByKey (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/core/di/reflective_injector.js:770:25)
at ReflectiveInjector_.get (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/core/di/reflective_injector.js:579:21)
at TestComponentBuilder.createAsync (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/testing/test_component_builder.js:185:52)
at eval (http://localhost:9876/spec/campaignList.component.spec.js:53:20)
at Object.eval (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/testing/testing.js:113:13)
在尝试 运行 这个甚至没有 expect
的测试时(比实际到达那里更快地崩溃):
import {
it, iit, xit, describe, ddescribe, xdescribe, expect, beforeEach, inject,
async, tick, withProviders, beforeEachProviders, TestComponentBuilder, fakeAsync
} from 'angular2/testing';
import { CampaignList } from 'components/campaign/campaignList.component'
import { CampaignService } from 'services/campaign.service'
import { provide } from 'angular2/core';
class MockCampaignService extends CampaignService {
create() {
return Promise.resolve(true);
}
}
describe('CampaignList: component', () => {
let tcb;
beforeEachProviders(() => [
TestComponentBuilder,
provide(CampaignService, {useClass: MockCampaignService}),
CampaignList
]);
beforeEach(inject([TestComponentBuilder], _tcb => {
tcb = _tcb
}));
it('should render `Hello World!`', done => {
return tcb.createAsync(CampaignList).then(fixture => {
fixture.detectChanges();
var compiled = fixture.debugElement.nativeElement;
})
.catch(e => done.fail(e))
})
})
这是我的 karma.config.js:
/* global module */
module.exports = function (config) {
'use strict';
var glob = require("glob");
var filesToServe = glob.sync("./app/**/*.@(js|ts|css|scss|html)");
var specsToLoad = glob.sync("./spec/**/*.@(spec.js)").map(function(file){
return file.substr(2);
});
config.set({
autoWatch: true,
singleRun: true,
frameworks: ['jspm', 'jasmine'],
files: [
'node_modules/babel-polyfill/dist/polyfill.js',
{pattern: 'node_modules/zone.js/dist/zone.js', included: true, watched: false},
{pattern: 'node_modules/zone.js/dist/async-test.js', included: true, watched: false}
],
jspm: {
config: 'config.js',
serveFiles: filesToServe,
loadFiles: ['./base/test-setup.js'].concat(specsToLoad)
},
proxies: {
'/spec/': '/base/spec/',
'/app/': '/base/app/',
'/jspm_packages/': '/base/jspm_packages/'
},
browsers: ['Chrome'],
preprocessors: {
'app/**/*.js': ['babel', 'sourcemap', 'coverage'],
'spec/**/*.js': ['babel']
},
babelPreprocessor: {
options: {
sourceMap: 'inline'
},
sourceFileName: function(file) {
return file.originalPath;
}
},
reporters: ['coverage', 'progress'],
coverageReporter: {
instrumenters: {isparta: require('isparta')},
instrumenter: {
'app/*.js': 'isparta'
},
reporters: [
{
type: 'text-summary',
subdir: normalizationBrowserName
},
{
type: 'html',
dir: 'coverage/',
subdir: normalizationBrowserName
}
]
}
});
function normalizationBrowserName(browser) {
return browser.toLowerCase().split(/[ /-]/)[0];
}
};
这是我的 test-setup.js
:
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);
有谁知道为什么会这样或者我应该做什么?
我想通了:test-setup.js
实际上被发现但没有被 babel 转译,因为它在 root
路径中,而只有 app
或 spec
中的文件被转译
preprocessors: {
'app/**/*.js': ['babel', 'sourcemap', 'coverage'],
'spec/**/*.js': ['babel']
},
所以我只是将测试设置移到 spec 文件夹中并将路径更改为此
jspm: {
loadFiles: ['spec/test-setup.js'].concat(specsToLoad)
}
然后一切开始工作
我收到以下错误:
Error: No provider for DirectiveResolver!
Error: DI Exception
at NoProviderError.BaseException [as constructor] (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/facade/exceptions.js:19:23)
at NoProviderError.AbstractProviderError [as constructor] (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/core/di/reflective_exceptions.js:41:16)
at new NoProviderError (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/core/di/reflective_exceptions.js:77:16)
at ReflectiveInjector_._throwOrNull (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/core/di/reflective_injector.js:779:19)
at ReflectiveInjector_._getByKeyDefault (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/core/di/reflective_injector.js:807:25)
at ReflectiveInjector_._getByKey (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/core/di/reflective_injector.js:770:25)
at ReflectiveInjector_.get (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/core/di/reflective_injector.js:579:21)
at TestComponentBuilder.createAsync (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/testing/test_component_builder.js:185:52)
at eval (http://localhost:9876/spec/campaignList.component.spec.js:53:20)
at Object.eval (http://localhost:9876/jspm_packages/npm/angular2@2.0.0-beta.17/src/testing/testing.js:113:13)
在尝试 运行 这个甚至没有 expect
的测试时(比实际到达那里更快地崩溃):
import {
it, iit, xit, describe, ddescribe, xdescribe, expect, beforeEach, inject,
async, tick, withProviders, beforeEachProviders, TestComponentBuilder, fakeAsync
} from 'angular2/testing';
import { CampaignList } from 'components/campaign/campaignList.component'
import { CampaignService } from 'services/campaign.service'
import { provide } from 'angular2/core';
class MockCampaignService extends CampaignService {
create() {
return Promise.resolve(true);
}
}
describe('CampaignList: component', () => {
let tcb;
beforeEachProviders(() => [
TestComponentBuilder,
provide(CampaignService, {useClass: MockCampaignService}),
CampaignList
]);
beforeEach(inject([TestComponentBuilder], _tcb => {
tcb = _tcb
}));
it('should render `Hello World!`', done => {
return tcb.createAsync(CampaignList).then(fixture => {
fixture.detectChanges();
var compiled = fixture.debugElement.nativeElement;
})
.catch(e => done.fail(e))
})
})
这是我的 karma.config.js:
/* global module */
module.exports = function (config) {
'use strict';
var glob = require("glob");
var filesToServe = glob.sync("./app/**/*.@(js|ts|css|scss|html)");
var specsToLoad = glob.sync("./spec/**/*.@(spec.js)").map(function(file){
return file.substr(2);
});
config.set({
autoWatch: true,
singleRun: true,
frameworks: ['jspm', 'jasmine'],
files: [
'node_modules/babel-polyfill/dist/polyfill.js',
{pattern: 'node_modules/zone.js/dist/zone.js', included: true, watched: false},
{pattern: 'node_modules/zone.js/dist/async-test.js', included: true, watched: false}
],
jspm: {
config: 'config.js',
serveFiles: filesToServe,
loadFiles: ['./base/test-setup.js'].concat(specsToLoad)
},
proxies: {
'/spec/': '/base/spec/',
'/app/': '/base/app/',
'/jspm_packages/': '/base/jspm_packages/'
},
browsers: ['Chrome'],
preprocessors: {
'app/**/*.js': ['babel', 'sourcemap', 'coverage'],
'spec/**/*.js': ['babel']
},
babelPreprocessor: {
options: {
sourceMap: 'inline'
},
sourceFileName: function(file) {
return file.originalPath;
}
},
reporters: ['coverage', 'progress'],
coverageReporter: {
instrumenters: {isparta: require('isparta')},
instrumenter: {
'app/*.js': 'isparta'
},
reporters: [
{
type: 'text-summary',
subdir: normalizationBrowserName
},
{
type: 'html',
dir: 'coverage/',
subdir: normalizationBrowserName
}
]
}
});
function normalizationBrowserName(browser) {
return browser.toLowerCase().split(/[ /-]/)[0];
}
};
这是我的 test-setup.js
:
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);
有谁知道为什么会这样或者我应该做什么?
我想通了:test-setup.js
实际上被发现但没有被 babel 转译,因为它在 root
路径中,而只有 app
或 spec
中的文件被转译
preprocessors: {
'app/**/*.js': ['babel', 'sourcemap', 'coverage'],
'spec/**/*.js': ['babel']
},
所以我只是将测试设置移到 spec 文件夹中并将路径更改为此
jspm: {
loadFiles: ['spec/test-setup.js'].concat(specsToLoad)
}
然后一切开始工作