2016-09-30 12 views
5

में .html लोड करने में असफल रहा कोणीय 2.0.0कोणीय 2.0.0 परीक्षण

beforeEach(async(() => { 
     TestBed.configureTestingModule({ 
      declarations: [ 
       SomethingComponent 
      ] 
     }).compileComponents(); // compile template 
    })); 

    beforeEach(() => { 
     fixture = TestBed.createComponent(SomethingComponent); 
     comp = fixture.componentInstance; 
    }); 

    it('test', async(() => { 
     fixture.detectChanges(); 
     expect(true) 
    })); 

घटक को देखते हुए में टेस्टबेड साथ एक समस्या है:

@Component({ 
    templateUrl: 'app/components/something/something.component.html' 
}) 

मैं त्रुटि मिलती है:

Firefox 48.0.0 (Mac OS X 10.11.0) 
Failed: Uncaught (in promise): Failed to load app/components/something/something.component.html 
[email protected]_modules/zone.js/dist/zone.js:429:31 
[email protected]_modules/zone.js/dist/zone.js:414:17 
scheduleResolveOrReject/<@node_modules/zone.js/dist/zone.js:462:17 
[email protected]_modules/zone.js/dist/zone.js:236:23 
[email protected]_modules/zone.js/dist/proxy.js:96:20 
[email protected]_modules/zone.js/dist/zone.js:235:23 
[email protected]_modules/zone.js/dist/zone.js:136:28 
[email protected]_modules/zone.js/dist/zone.js:368:25 
ZoneTask/[email protected]_modules/zone.js/dist/zone.js:308:25 

मुझे लगता है कि ऐसा इसलिए हो सकता है क्योंकि कॉन्फ़िगरेशन सही नहीं है। मैं घूंट उपयोग कर रहा हूँ जावास्क्रिप्ट में टाइपप्रति precompile करने के लिए, और मैं निम्नलिखित परियोजना संरचना है:

|_ grails-app 
|_ frontend 
    |_ angular 
    |_ app 
    | |_ components 
    | |  |_ something 
    | |   |_ something.component.ts 
    | |   |_ something.component.html 
    | |_ model 
    | |_ test 
    | |_ app.module.ts 
    | |_ app.routing.ts 
    | |_ main.ts 
    |_ assets 
    |_ build 
    | |_ app 
    |  |_ components 
    |  |  |_ something 
    |  |   |_ something.component.html 
    |  |   |_ something.component.js 
    |  |   |_ something.component.js.map 
    |  |_  
    |  |_  
    |  |  
    |_ node_modules 
    |_ gulpfile.js 
    |_ karma.conf.js 
    |_ karma-test-shim.js 
    |_ systemjs.config 
    |_ 
    |_ 
    | 

लेकिन अगर मुझे लगता है कि html फ़ाइल precompiled है, और परीक्षण से आदेश .compileComponents() निकालने के लिए, मैं निम्नलिखित त्रुटि मिलती है :

Firefox 48.0.0 (Mac OS X 10.11.0) 
Error: Cannot create the component SomethingComponent as it was not imported into the testing module! 

और अगर मैं async निकालने के लिए, मैं इस त्रुटि मिलती है:

Firefox 48.0.0 (Mac OS X 10.11.0) 
Error: This test module uses the component SomethingComponent which is using a "templateUrl", but they were never compiled. Please call "TestBed.compileComponents" before your test. 

karma-test-shim.js में, मैं है:

System.config({ 
baseURL: '/base/', 
paths: { 
    // paths serve as alias 
    'npm:': 'node_modules/' 
}, 
map: { 
    'app': '/base/build/app/', 
    '@angular/core': 'npm:@angular/core/bundles/core.umd.js', 
    '@angular/common': 'npm:@angular/common/bundles/common.umd.js', 
    '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js', 
    '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js', 
    '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', 
    '@angular/http': 'npm:@angular/http/bundles/http.umd.js', 
    '@angular/router': 'npm:@angular/router/bundles/router.umd.js', 
    '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js', 

    // angular testing umd bundles 
    '@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js', 
    '@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js', 
    '@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js', 
    '@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js', 
    '@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js', 
    '@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js', 
    '@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js', 
    '@angular/forms/testing': 'npm:@angular/forms/bundles/forms-testing.umd.js', 

    // other libraries 
    'rxjs': 'npm:rxjs' 
}, 
packages: { 
    'app': { 
     defaultExtension: 'js' 
    }, 
    'rxjs': { 
     defaultExtension: 'js' 
    } 
} 
}); 

मुझे उम्मीद है कि कोई मुझे यह समझने में मदद कर सकता है कि मैं कॉन्फ़िगरेशन में गलत क्या कर रहा हूं।

उत्तर

2

मुझे एक समाधान मिला।

beforeEach(async(() => { 
    TestBed.configureTestingModule({ 
     declarations: [ 
      SomethingComponent 
     ] 
    }) 
    .overrideComponent(SomethingComponent, { 
     set: { 
      templateUrl: '/base/build/app/components/something/something.component.html' 
     }} 
    ) 
    .compileComponents(); // compile template 
})); 

आप सोच सकता है कि आप '/ आधार/निर्माण /' कहीं उपसर्ग परिभाषित करने के लिए सक्षम होना चाहिए। हालांकि, मैं अभी तक इसका पता लगाने में सक्षम नहीं हूं।

4

आप आधार पथ मैं एक प्रॉक्सी के रूप में karma.conf.js परिभाषित कर सकते हैं:

// Proxied base paths for loading assets 
proxies: { 
    // required for component assets fetched by Angular's compiler 
    "/app/": '/base/build/app/' 
}, 

स्रोत: https://github.com/angular/quickstart

+0

ओवरराइड करने के लिए कर्मा 1.1.2 के लिए मैं पूरी प्रॉक्सी पथ निर्दिष्ट किया था, यानी '/ आधार/ऐप्स /' हैं: '/ आधार/निर्माण/ऐप्स'। –

0

इस प्रयास करें। मेरे लिए ठीक काम करता है। टेम्पलेट को ओवरराइड करने की आवश्यकता नहीं है और कर्म में कोई रास्ता निर्धारित नहीं किया है।

describe('test something',() => { 
//mock service if need to or ignore it 
    class MockService { 

     getActionOutput(body: string) { 
      return Observable.from(['something']); 
     } 
    } 
    beforeEach(() => { 
     TestBed.configureTestingModule({ 
      declarations: [ 
       SomethingComponent 
      ], 
      imports: [ 
       // HttpModule, etc. 
      ], 
      providers: [ 
       BaseRequestOptions, 
       MockBackend, 
       { 
        provide: Http, 
        useFactory: function(backend: ConnectionBackend, defaultOptions: BaseRequestOptions) { 
         return new Http(backend, defaultOptions); 
        }, 
        deps: [MockBackend, BaseRequestOptions] 
       }, 
       //Mock service if need to or ignore it 
       {provide: SomeService, useClass: MockService} 
     ] 
     }); 
     fixture = TestBed.createComponent(SomeComponent); 
     component = fixture.componentInstance; 
    }); 
    it('should work something', async(() => { 
     expect(component).toBeDefined(); 
    })); 
}); 
मेरे लिए

यह बात नहीं है जहां उर टेम्पलेट html .. जब तक यू यह

संबंधित मुद्दे