2016-04-06 43 views
0

我有一個「注射」的服務,我想爲做一個基本的測試:無法加載所注入的服務測試

login.service.ts

進口{}注射從「angular2 /核心「; import'Http,Response} from'angular2/http';

@Injectable() 
export class LoginService { 

    constructor(private http: Http) { } 
    response: JSON; 
    getPartners() { 
     return this.http.get('http://bla/bla/partners') 
         .map((res: Response) => res.json()); 
    } 
} 

現在我的測試: login.service.spec.ts

import { it, iit, describe, expect, inject, injectAsync, beforeEachProviders, fakeAsync, tick } from 'angular2/testing'; 
import { provide } from 'angular2/core'; 
import { LoginService } from './login.service'; 



describe('login service',() => { 
    beforeEachProviders(() => [LoginService]) 

    it('should get partners', inject([LoginService], (service) => { 
    console.log('HERE ', service) 
    expect(true).toBe(true); 
    // todo expect(service.getPartners())... 
    })); 

我不能去任何地方,但因爲我得到一個404: 無法加載資源:服務器與狀態迴應的404(未找到) http://localhost:9876/base/dist/components/login/login.service 我可能錯過了一些基本的東西,因爲我得到這個問題的任何類我需要測試,但不是爲接口。

我的人緣試驗shim.js:

// Tun on full stack traces in errors to help debugging 
Error.stackTraceLimit = Infinity; 


jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000; 

// // Cancel Karma's synchronous start, 
// // we will call `__karma__.start()` later, once all the specs are loaded. 
__karma__.loaded = function() { }; 


System.config({ 
    packages: { 
     'base/src': { 
      defaultExtension: false, 
      format: 'register', 
      map: Object.keys(window.__karma__.files). 
       filter(onlyAppFiles). 
       reduce(function createPathRecords(pathsMapping, appPath) { 
        // creates local module name mapping to global path with karma's fingerprint in path, e.g.: 
        // './hero.service': '/base/src/app/hero.service.js?f4523daf879cfb7310ef6242682ccf10b2041b3e' 
        var moduleName = appPath.replace(/^\/base\/src\//, './').replace(/\.js$/, ''); 
        pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath] 
        return pathsMapping; 
       }, {}) 

     } 
    } 
}); 

System.import('angular2/src/platform/browser/browser_adapter').then(function (browser_adapter) { 
    browser_adapter.BrowserDomAdapter.makeCurrent(); 
}).then(function() { 
    return Promise.all(
     Object.keys(window.__karma__.files) // All files served by Karma. 
      .filter(onlySpecFiles) 
     // .map(filePath2moduleName)  // Normalize paths to module names. 
      .map(function (moduleName) { 
       // loads all spec files via their global module names (e.g. 'base/src/app/hero.service.spec') 
       return System.import(moduleName); 
      })); 
}) 
    .then(function() { 
     __karma__.start(); 
    }, function (error) { 
     __karma__.error(error.stack || error); 
    }); 


function filePath2moduleName(filePath) { 
    return filePath. 
     replace(/^\//, '').    // remove/prefix 
     replace(/\.\w+$/, '');   // remove suffix 
} 


function onlyAppFiles(filePath) { 
    return /^\/base\/src\/.*\.js$/.test(filePath) 
} 


function onlySpecFiles(path) { 
    return /.spec\.js$/.test(path); 
} 

我karma.conf.js:

files: [ 
    // paths loaded by Karma 
    { pattern: 'node_modules/angular2/bundles/angular2-polyfills.js', included: true, watched: true }, 
    { pattern: 'node_modules/systemjs/dist/system.src.js', included: true, watched: true }, 
    { pattern: 'node_modules/rxjs/bundles/rx.js', included: true, watched: true }, 
    { pattern: 'node_modules/angular2/bundles/angular2.js', included: true, watched: true }, 
    { pattern: 'node_modules/angular2/bundles/testing.dev.js', included: true, watched: true }, 
    { pattern: 'karma-test-shim.js', included: true, watched: true }, 
    { pattern: 'dist/components/matchers.js', included: true, watched: true }, 

    // paths loaded via module imports 
    { pattern: 'dist/components/**/*.js', included: false, watched: true }, 

    // paths loaded via Angular's component compiler 
    // (these paths need to be rewritten, see proxies section) 
    { pattern: 'dist/*.html', included: false, watched: true }, 
    { pattern: 'dist/styles/*.css', included: false, watched: true }, 
    { pattern: 'dist/components/**/*.html', included: false, watched: true }, 
    { pattern: 'dist/components/**/*.css', included: false, watched: true }, 

    // paths to support debugging with source maps in dev tools 
    { pattern: 'src/components/**/*.ts', included: false, watched: false }, 
    { pattern: 'dist/components/**/*.js.map', included: false, watched: false } 
], 

其中DIST是我的build文件夾

+0

你可以添加你的'karma-test-shim.js'文件嗎?我認爲問題在於SystemJS配置的級別。謝謝! –

+0

謝謝!這是我的業力測試shim.js: –

+0

你可以把它放到你的問題?謝謝;-) –

回答

1

我認爲你的配置應該karma-test-shim.js文件中的以下內容:

System.config({ 
    packages: { 
    'base/dist': { // <------------- 
     defaultExtension: false, 
     format: 'register', 
     map: Object.keys(window.__karma__.files). 
      filter(onlyAppFiles). 
     (...) 
+0

也嘗試過,同樣的錯誤:angular2-polyfills.js:469未處理的Promise拒絕:__karma __。錯誤不是函數;區域:;任務:Promise.then;值:TypeError:__karma __。error不是函數(...)consoleError @ angular2-polyfills.js:469 angular2-polyfills.js:471錯誤:未捕獲(在promise中):TypeError:__karma __。error不是函數(... )consoleError @ angular2-polyfills.js:471 http:// localhost:9876/base/dist/components/login/login.service加載資源失敗:服務器響應狀態爲404(未找到) –

+0

你'createPathRecords'方法產生?我的意思是映射(模塊名稱/目標文件)。 –

+0

我認爲它映射文件路徑(Karma)以便能夠拾取它們。我只是看着angular2-test-seed應用程序,是同一件事... –