本文整理汇总了TypeScript中@angular/compiler/testing/src/resource_loader_mock.MockResourceLoader类的典型用法代码示例。如果您正苦于以下问题:TypeScript MockResourceLoader类的具体用法?TypeScript MockResourceLoader怎么用?TypeScript MockResourceLoader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MockResourceLoader类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: createSummaries
function createSummaries() {
const resourceLoader = new MockResourceLoader();
setMetadata(resourceLoader);
TestBed.configureCompiler({providers: [{provide: ResourceLoader, useValue: resourceLoader}]});
TestBed.configureTestingModule({imports: [SomeModule], providers: [SomeDep]});
let summariesPromise = TestBed.compileComponents().then(() => {
const metadataResolver = TestBed.get(CompileMetadataResolver) as CompileMetadataResolver;
const summaries = [
metadataResolver.getNgModuleSummary(SomeModule),
// test nesting via closures, as we use this in the generated code too.
() =>
[metadataResolver.getDirectiveSummary(SomePublicComponent),
metadataResolver.getDirectiveSummary(SomePrivateComponent),
],
metadataResolver.getDirectiveSummary(SomeDirective),
metadataResolver.getPipeSummary(SomePipe),
metadataResolver.getInjectableSummary(SomeService)
];
clearMetadata();
TestBed.resetTestingModule();
return () => summaries;
});
resourceLoader.flush();
return summariesPromise;
}
示例2: setMetadata
function setMetadata(resourceLoader: MockResourceLoader) {
Base.parameters = [[SomeDep]];
SomeModule.annotations = [new NgModule({
declarations: [SomePublicComponent, SomePrivateComponent, SomeDirective, SomePipe],
exports: [SomeDirective, SomePipe, SomePublicComponent],
providers: [SomeService]
})];
SomePublicComponent.annotations = [new Component({templateUrl: 'somePublicUrl.html'})];
resourceLoader.expect('somePublicUrl.html', `Hello public world!`);
SomePrivateComponent.annotations = [new Component({templateUrl: 'somePrivateUrl.html'})];
resourceLoader.expect('somePrivateUrl.html', `Hello private world!`);
SomeDirective.annotations = [new Directive({selector: '[someDir]'})];
SomePipe.annotations = [new Pipe({name: 'somePipe'})];
SomeService.annotations = [new Injectable()];
}
示例3: describe
describe('MockResourceLoader', () => {
let resourceLoader: MockResourceLoader;
beforeEach(() => { resourceLoader = new MockResourceLoader(); });
function expectResponse(
request: Promise<string>, url: string, response: string, done: () => void = null) {
function onResponse(text: string): string {
if (response === null) {
throw `Unexpected response ${url} -> ${text}`;
} else {
expect(text).toEqual(response);
if (done != null) done();
}
return text;
}
function onError(error: string): string {
if (response !== null) {
throw `Unexpected error ${url}`;
} else {
expect(error).toEqual(`Failed to load ${url}`);
if (done != null) done();
}
return error;
}
request.then(onResponse, onError);
}
it('should return a response from the definitions',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const url = '/foo';
const response = 'bar';
resourceLoader.when(url, response);
expectResponse(resourceLoader.get(url), url, response, () => async.done());
resourceLoader.flush();
}));
it('should return an error from the definitions',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const url = '/foo';
const response: string = null;
resourceLoader.when(url, response);
expectResponse(resourceLoader.get(url), url, response, () => async.done());
resourceLoader.flush();
}));
it('should return a response from the expectations',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const url = '/foo';
const response = 'bar';
resourceLoader.expect(url, response);
expectResponse(resourceLoader.get(url), url, response, () => async.done());
resourceLoader.flush();
}));
it('should return an error from the expectations',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const url = '/foo';
const response: string = null;
resourceLoader.expect(url, response);
expectResponse(resourceLoader.get(url), url, response, () => async.done());
resourceLoader.flush();
}));
it('should not reuse expectations', () => {
const url = '/foo';
const response = 'bar';
resourceLoader.expect(url, response);
resourceLoader.get(url);
resourceLoader.get(url);
expect(() => { resourceLoader.flush(); }).toThrowError('Unexpected request /foo');
});
it('should return expectations before definitions',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const url = '/foo';
resourceLoader.when(url, 'when');
resourceLoader.expect(url, 'expect');
expectResponse(resourceLoader.get(url), url, 'expect');
expectResponse(resourceLoader.get(url), url, 'when', () => async.done());
resourceLoader.flush();
}));
it('should throw when there is no definitions or expectations', () => {
resourceLoader.get('/foo');
expect(() => { resourceLoader.flush(); }).toThrowError('Unexpected request /foo');
});
it('should throw when flush is called without any pending requests', () => {
expect(() => { resourceLoader.flush(); }).toThrowError('No pending requests to flush');
});
it('should throw on unsatisfied expectations', () => {
resourceLoader.expect('/foo', 'bar');
resourceLoader.when('/bar', 'foo');
resourceLoader.get('/bar');
expect(() => { resourceLoader.flush(); }).toThrowError('Unsatisfied requests: /foo');
});
//.........这里部分代码省略.........
示例4: inject
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const url = '/foo';
const response: string = null;
resourceLoader.expect(url, response);
expectResponse(resourceLoader.get(url), url, response, () => async.done());
resourceLoader.flush();
}));
示例5: it
it('should not reuse expectations', () => {
const url = '/foo';
const response = 'bar';
resourceLoader.expect(url, response);
resourceLoader.get(url);
resourceLoader.get(url);
expect(() => { resourceLoader.flush(); }).toThrowError('Unexpected request /foo');
});
示例6: expect
expect(() => { resourceLoader.flush(); }).toThrowError('Unexpected request /foo');