2016-11-04 4 views
6

मैं इस त्रुटि हो रही है:कोणीय 2 और जैस्मीन: त्रुटि: अपने परीक्षण से पहले "TestBed.compileComponents" कॉल करें

Error: This test module uses the component MessagesComponent which is using a "templateUrl", but they were never compiled. Please call "TestBed.compileComponents" before your test. 

जब यह सरल कोणीय परीक्षण चलाने की कोशिश कर 2 & जैस्मीन टेस्ट:

let comp: MessagesComponent; 
let fixture: ComponentFixture<MessagesComponent>; 

describe('MessagesComponent',() => { 
    beforeEach(() => { 


     TestBed.configureTestingModule({ 
      declarations: [ MessagesComponent ], 
      providers: [ {provide: DataService, useValue: {} } ] 

     }) 
      .compileComponents(); // compile template and css 

     fixture = TestBed.createComponent(MessagesComponent); 
     comp = fixture.componentInstance; 

    }); 

    it('example',() => { 
     expect("true").toEqual("true"); 
    }); 
}); 

मुझे लगता है कि यह मेरे webpack परीक्षण विन्यास के साथ कुछ की वजह से हो सकता है:

'use strict'; 

const path = require('path'); 
const webpack = require('webpack'); 

module.exports = { 
    devtool: 'inline-source-map', 
    module: { 
     loaders: [ 
      { loader: 'raw', test: /\.(css|html)$/ }, 
      { exclude: /node_modules/, loader: 'ts', test: /\.ts$/ } 
     ] 
    }, 
    resolve: { 
     extensions: ['', '.js', '.ts'], 
     modulesDirectories: ['node_modules'], 
     root: path.resolve('.', 'src') 
    }, 
    tslint: { 
     emitErrors: true 
    } 
}; 

उत्तर

15

टेम्पलेट फ़ेचिंग असीमित है जब आपके टेम्पलेट्स आपके घटकों में रेखांकित नहीं होते हैं, इसलिए आपको जैस्मीन को बताना होगा। क्योंकि webpack inlines टेम्पलेट्स और सीएसएस के हिस्से के रूप चूंकि आप पहले से webpack उपयोग कर रहे हैं

beforeEach(() => { 
    TestBed.configureTestingModule({ ... }) 
     .compileComponents(); 
    fixture = TestBed.createComponent(MessagesComponent); 
    comp = fixture.componentInstance; 
}); 

beforeEach(async(() => { 
    TestBed.configureTestingModule({ ... }) 
     .compileComponents() 
     .then(() => { 
      fixture = TestBed.createComponent(MessagesComponent); 
      comp = fixture.componentInstance; 
     }); 
})); 
+0

क्या मुझे इसे किसी भी तरह से (...) विधि बदलनी चाहिए? – commonSenseCode

+0

केवल तभी यदि यह '() 'असीमित रूप से कुछ भी करता है, उदाहरण के लिए, यदि यह किसी सेवा से डेटा प्राप्त करता है। यह पूरी तरह से ठीक है अगर 'पहले से)() को अतुल्यकालिक होना चाहिए लेकिन 'it()' नहीं है। – pe8ter

+0

अजीब मैंने पूछा क्योंकि मैं अपने मूल प्रश्न में पोस्ट किए गए एक ही त्रुटि संदेश को प्राप्त करता हूं लेकिन कम से कम अब यह कहता है कि 1 में से 1 विफल रहा – commonSenseCode

0

के लिए परिवर्तित करें, सैद्धांतिक रूप से आप आधिकारिक दस्तावेज़ here के अनुसार compileComponents() समारोह कॉल करने के लिए नहीं होना चाहिए, स्वचालित निर्माण प्रक्रिया जो परीक्षण चलाने से पहले होती है।

एक संभावित कारण यह है कि आपके टेम्पलेट/सीएसएस inlined नहीं कर रहे हैं आईडीई (VisualStudio/WebStorm/IntelliJ) ऑटो js और webpack लोडर जो js/ts फ़ाइलों के लिए लक्षित कर के बजाय js पहले से ही संकलित फ़ाइलों पर लागू करने के लिए कोशिश कर रहे हैं करने के लिए अपने ts संकलित है स्रोत टीएस फाइलों के।

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