๋ฐ๋ชจ ๋ชฉ์ ์ผ๋ก ๋จ์ํ API๋ฅผ ๋
ธ์ถํ๊ณ ์ผ๋ฐ์ ์ธ ๊ตฌ์ฑ ์์ ํ
์คํธ๋ฅผ ์ํํ๋ ๋ฐ ํ์ํ ๊ธฐํธ๋ฅผ ๊ฐ์ ธ์ค๋ ๊ตฌ์ฑ ์์ MyComponent
๊ฐ ์ ๊ณต๋ฉ๋๋ค.
import MyComponent from './my.component';
import {
describe,
expect,
it,
inject,
beforeEach,
beforeEachProviders } from '@angular/core/testing';
import { TestComponentBuilder } from '@angular/compiler/testing';
๋ค์๊ณผ ๊ฐ์ด MyComponent
์ ํ์ ๊ณ ์ ์ฅ์น ๊ตฌ์ฑ ์์๋ฅผ ๋ง๋ค๊ธฐ ์ํด TestComponentBuilder๋ฅผ ์ฃผ์
ํ๊ณ ์ธ์คํด์คํํ๋ ๊ธฐ๋ณธ ํ
์คํธ๋ฅผ ์ํํ๋ ค๊ณ ํฉ๋๋ค.
describe('MyComponent', () => {
let testComponentBuilder: TestComponentBuilder;
beforeEachProviders(() => [TestComponentBuilder]);
beforeEach(inject([TestComponentBuilder], (_testComponentBuilder: TestComponentBuilder) => {
testComponentBuilder = _testComponentBuilder;
}
));
it('should feature this or that behavior', done => {
// We create a test component fixture on runtime out from the component symbol
testComponentBuilder.createAsync(MyComponent).then(componentFixture => {
// Here we proceed to run assertions on the componentFixture
// ...
done();
})
.catch(e => done.fail(e));
});
๋ด ์ฝ๋๋ฅผ RC.1๋ก ์ ๊ทธ๋ ์ด๋ํ ํ ํญ์ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
Error: No precompiled component MyComponent found
๊ธฐ๋ณธ์ ์ผ๋ก testComponentBuilder.createAsync(MyComponent)
์ํด ๋ฐํ๋ ์ฝ์์ ํด๊ฒฐ๋์ง ์์ต๋๋ค. ์ด ๋ฌธ์ ๋ TestComponentBuilder
์ฌ์ฉ์ ํฌํจํ๋ ๋ชจ๋ ํ
์คํธ์์ ์ผ๊ด๋ฉ๋๋ค.
๊ธฐ๋ก์ ์ํด ๊ทธ๋ฆฌ๊ณ ํ ์คํธ๋ฅผ ์ํํ๊ธฐ ์ ์ ์ด์ ์ ๋ค์๊ณผ ๊ฐ์ด ํ ์คํธ ๊ณต๊ธ์๋ฅผ ๊ตฌ์ฑํ์ต๋๋ค.
import { setBaseTestProviders } from '@angular/core/testing';
import {
TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
TEST_BROWSER_STATIC_APPLICATION_PROVIDERS
} from '@angular/platform-browser/testing';
setBaseTestProviders(
TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
TEST_BROWSER_STATIC_APPLICATION_PROVIDERS
);
๋๋ ์ด๊ฒ์ด RC.1์ ์ค์ ๋ฒ๊ทธ์ธ์ง ์๋๋ฉด ์ฑ๊ณตํ์ง ๋ชปํ ๋ด ํ ์คํธ ์ค์ ์ ๋ฌธ์ ์ธ์ง ํ์ธํ๊ธฐ ์ํด ๋์ ์์ค๊ณผ ๋ฎ์ ์์ค์ ์ฝ๋๋ฅผ ์ค์บํ์ต๋๋ค. RC.1 ์ด์ ์๋ ๋ด ํ ์คํธ ์ค์ ์ด ๋ชจ๋ ์ฌ์ ํ ์คํธ ๊ตฌ์ฑ ์์์์ ์ํํ๊ฒ ์๋ํ์ต๋๋ค.
์ด๋ค ํฌ์ธํฐ?
๋๋ ์ด๊ฒ์ ์ค์ค๋ก ๊ณ ์น ์ ์์๋ค. ๋ด ์ ํ๋ฆฌ์ผ์ด์ ๋ฐ ํ๋ซํผ ํ ์คํธ ์ ๊ณต์ ์ฒด ์ค์ ์ด ์๋ชป๋ ๊ฒ์ผ๋ก ๋ํ๋ฌ์ต๋๋ค. ๋ค์๊ณผ ๊ฐ์ด ์ค์ ์คํฌ๋ฆฝํธ๋ฅผ ๊ตฌ์ฑํ๋ฉด ํธ๋ฆญ์ ์ํํ์ต๋๋ค.
import { resetBaseTestProviders, setBaseTestProviders } from '@angular/core/testing';
import { BROWSER_APP_DYNAMIC_PROVIDERS } from "@angular/platform-browser-dynamic";
import {
TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
ADDITIONAL_TEST_BROWSER_PROVIDERS
} from '@angular/platform-browser/testing';
resetBaseTestProviders();
setBaseTestProviders(
TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
[
BROWSER_APP_DYNAMIC_PROVIDERS,
ADDITIONAL_TEST_BROWSER_PROVIDERS
]
);
๋ด ์๋ชป์ด์ผ! ;)
@deeleman ng-cli ๋ฐ https://github.com/juliemr/ng2-test-seed/blob/master/karma-test-shim.js ์์ ์ฌ์ฉํ๋ ๊ธฐ๋ณธ ์ค์ ๊ณผ ๋์ผํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค. (
/*global jasmine, __karma__, window*/
Error.stackTraceLimit = Infinity;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;
__karma__.loaded = function () {
};
var distPath = '/base/.test/';
var appPath = distPath + 'app/';
function isJsFile(path) {
return path.slice(-3) == '.js';
}
function isSpecFile(path) {
return path.slice(-8) == '.spec.js';
}
function isAppFile(path) {
return isJsFile(path) && (path.substr(0, appPath.length) == appPath);
}
var allSpecFiles = Object.keys(window.__karma__.files)
.filter(isSpecFile)
.filter(isAppFile);
// Load our SystemJS configuration.
System.config({
baseURL: distPath,
});
System.import('system.config.js').then(function(e) {
// Load and configure the TestComponentBuilder.
return Promise.all([
System.import('@angular/core/testing'),
System.import('@angular/platform-browser-dynamic/testing')
]).then(function (providers) {
var testing = providers[0];
var testingBrowser = providers[1];
testing.setBaseTestProviders(
testingBrowser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
testingBrowser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS
);
});
}).then(function() {
return Promise.all(
allSpecFiles.map(function (moduleName) {
return System.import(moduleName);
}));
}).then(__karma__.start, __karma__.error);
์ด ๋ฌธ์ ๋ ํ๋์ด ์์ด ์๋์ผ๋ก ์ ๊ฒผ์ต๋๋ค.
์ ์ฌํ๊ฑฐ๋ ๊ด๋ จ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ฉด ์ ๋ฌธ์ ๋ฅผ ์ ์ถํ์ญ์์ค.
์๋ ๋ํ ์ ๊ธ ์ ์ฑ ์ ๋ํด ์์ธํ ์์๋ณด์ธ์.
_์ด ์์ ์ ๋ด์ ์ํด ์๋์ผ๋ก ์ํ๋์์ต๋๋ค._
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋๋ ์ด๊ฒ์ ์ค์ค๋ก ๊ณ ์น ์ ์์๋ค. ๋ด ์ ํ๋ฆฌ์ผ์ด์ ๋ฐ ํ๋ซํผ ํ ์คํธ ์ ๊ณต์ ์ฒด ์ค์ ์ด ์๋ชป๋ ๊ฒ์ผ๋ก ๋ํ๋ฌ์ต๋๋ค. ๋ค์๊ณผ ๊ฐ์ด ์ค์ ์คํฌ๋ฆฝํธ๋ฅผ ๊ตฌ์ฑํ๋ฉด ํธ๋ฆญ์ ์ํํ์ต๋๋ค.
๋ด ์๋ชป์ด์ผ! ;)