Angular2单元测试组件获得“No DirectiveResolver没有提供者”

问题描述 投票:0回答:1

我收到以下错误:

Error: No provider for DirectiveResolver!
Error: DI Exception
    at NoProviderError.BaseException [as constructor] (http://localhost:9876/jspm_packages/npm/[email protected]/src/facade/exceptions.js:19:23)
    at NoProviderError.AbstractProviderError [as constructor] (http://localhost:9876/jspm_packages/npm/[email protected]/src/core/di/reflective_exceptions.js:41:16)
    at new NoProviderError (http://localhost:9876/jspm_packages/npm/[email protected]/src/core/di/reflective_exceptions.js:77:16)
    at ReflectiveInjector_._throwOrNull (http://localhost:9876/jspm_packages/npm/[email protected]/src/core/di/reflective_injector.js:779:19)
    at ReflectiveInjector_._getByKeyDefault (http://localhost:9876/jspm_packages/npm/[email protected]/src/core/di/reflective_injector.js:807:25)
    at ReflectiveInjector_._getByKey (http://localhost:9876/jspm_packages/npm/[email protected]/src/core/di/reflective_injector.js:770:25)
    at ReflectiveInjector_.get (http://localhost:9876/jspm_packages/npm/[email protected]/src/core/di/reflective_injector.js:579:21)
    at TestComponentBuilder.createAsync (http://localhost:9876/jspm_packages/npm/[email protected]/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/[email protected]/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);

有谁知道为什么会这样或者我应该做什么?

angular karma-jasmine jspm angular2-testing isparta
1个回答
1
投票

我明白了:test-setup.js实际上被发现但没有被babel描述,因为它是在root路径而只有appspec的文件被编译

    preprocessors: {
        'app/**/*.js': ['babel', 'sourcemap', 'coverage'],
        'spec/**/*.js': ['babel']
    },

所以我只是在spec文件夹中移动了测试设置并将路径更改为此

    jspm: {
        loadFiles: ['spec/test-setup.js'].concat(specsToLoad)
    }

一切都开始奏效了

© www.soinside.com 2019 - 2024. All rights reserved.