本文整理汇总了TypeScript中angular2/testing.beforeEachProviders函数的典型用法代码示例。如果您正苦于以下问题:TypeScript beforeEachProviders函数的具体用法?TypeScript beforeEachProviders怎么用?TypeScript beforeEachProviders使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了beforeEachProviders函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: describe
describe('UserService', () => {
beforeEachProviders(() => [
UserService,
StorageService,
SessionService,
HttpService,
BaseRequestOptions,
MockBackend,
provide(Http, {
useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
})
]);
// beforeEach(inject([MockBackend], (backend: MockBackend) => {
// const baseResponse = new Response(new ResponseOptions({ body: { id: 1, email: 'a@a.com', username: 'a@a.com', password: 'a@a.com' } }));
// backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
// }));
it('should return token when authenticate worked',
inject([UserService, MockBackend], (userService: UserService, backend: MockBackend) => {
let token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJzZXJ2ZXIiLCJlbWFpbCI6ImFAYS5jb20iLCJwYXNzd29yZCI6ImFAYS5jb20iLCJpYXQiOjE0NTk0NDc4ODAsImV4cCI6MTQ1OTUzNDI4MH0.b260_KHB1FBBNlu2avblbi9VzqSER9hnzzCzdf6cGA4';
let baseResponse = new Response(new ResponseOptions({ body: { success: true, message: 'Enjoy your token!', token: token } }));
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
User.setNextId(0);
userService.authenticate('a@a.com', 'a@a.com', false).subscribe((res: any) => {
expect(res.token).toBe(token);
});
})
);
it('should return user when authenticate worked',
inject([UserService, MockBackend], (userService: UserService, backend: MockBackend) => {
let token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJzZXJ2ZXIiLCJlbWFpbCI6ImFAYS5jb20iLCJwYXNzd29yZCI6ImFAYS5jb20iLCJpYXQiOjE0NTk0NDc4ODAsImV4cCI6MTQ1OTUzNDI4MH0.b260_KHB1FBBNlu2avblbi9VzqSER9hnzzCzdf6cGA4';
let baseResponse = new Response(new ResponseOptions({ body: { success: true, message: 'Enjoy your token!', token: token } }));
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
User.setNextId(0);
userService.authenticate('a@a.com', 'a@a.com', false).subscribe((res: any) => {
expect(res.user.id).toBe(1);
expect(res.user.username).toBe('server');
expect(res.user.email).toBe('a@a.com');
});
})
);
it('should return a user without password when authenticate worked',
inject([UserService, MockBackend], (userService: UserService, backend: MockBackend) => {
let token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJzZXJ2ZXIiLCJlbWFpbCI6ImFAYS5jb20iLCJwYXNzd29yZCI6ImFAYS5jb20iLCJpYXQiOjE0NTk0NDc4ODAsImV4cCI6MTQ1OTUzNDI4MH0.b260_KHB1FBBNlu2avblbi9VzqSER9hnzzCzdf6cGA4';
let baseResponse = new Response(new ResponseOptions({ body: { success: true, message: 'Enjoy your token!', token: token } }));
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
User.setNextId(0);
userService.authenticate('a@a.com', 'a@a.com', false).subscribe((res: any) => {
console.log('HERE', res);
expect(res.user.password).toBe(undefined);
});
})
);
});
示例2: describe
describe('Blog Service', () => {
// All heed this block - it is required so that the test injector
// is properly set up. Without doing this, you won't get the
// fake backend injected into Http.
// Also, you need to inject MockBackend as a provider before you wire
// it to replace XHRBackend with the provide function! So this is all
// extremely important to set up right.
beforeEachProviders(() => {
return [
HTTP_PROVIDERS,
provide(XHRBackend, {useClass: MockBackend}),
BlogService
];
});
it('should get blogs', inject([XHRBackend, BlogService], (mockBackend, blogService) => {
mockBackend.connections.subscribe(
(connection: MockConnection) => {
connection.mockRespond(new Response(
new ResponseOptions({
body: [
{
id: 26,
contentRendered: "<p><b>Hi there</b></p>",
contentMarkdown: "*Hi there*"
}]
}
)));
});
blogService.getBlogs().subscribe((blogs: BlogEntry[]) => {
expect(blogs.length).toBe(1);
expect(blogs[0].id).toBe(26);
});
}));
it('should get blogs async', injectAsync([XHRBackend, BlogService], (mockBackend, blogService) => {
return new Promise((pass, fail) => {
mockBackend.connections.subscribe(
(connection: MockConnection) => {
connection.mockRespond(new Response(
new ResponseOptions({
body: [
{
id: 26,
contentRendered: "<p><b>Hi there</b></p>",
contentMarkdown: "*Hi there*"
}]
}
)));
});
blogService.getBlogs().subscribe(
(data) => {
expect(data.length).toBe(1);
expect(data[0].id).toBe(26);
expect(data[0].contentMarkdown).toBe('*Hi there*');
});
});
}), 3000);
it('should save updates to an existing blog entry',
injectAsync([XHRBackend, BlogService], (mockBackend, blogService) => {
return new Promise((resolve, reject) => {
mockBackend.connections.subscribe(connection => {
connection.mockRespond(new ResponseOptions({status: 200}));
});
let data: BlogEntry = new BlogEntry("Blog Entry", "<p><b>Hi</b></p>", "*Hi*", 10);
blogService.saveBlog(data).subscribe(
(successResult) => {
expect(successResult).toBeDefined( );
expect(successResult.status).toBe(200);
});
});
}), 300);
});
示例3: describe
describe('DemoFormWithEvents', () => {
var _console;
var fakeConsole;
var el, input, form;
beforeEach(() => {
// declare a fake console to track all the logs
fakeConsole = {};
fakeConsole._logs = [];
fakeConsole.log = (...theArgs) => fakeConsole._logs.push(theArgs.join(' '));
// replace the real console with our fake version
_console = window.console;
window.console = fakeConsole;
});
// restores the real console
afterAll(() => window.console = _console);
beforeEachProviders(() => {
return [FormBuilder];
});
function createComponent(tcb: TestComponentBuilder): Promise<ComponentFixture> {
return tcb.createAsync(DemoFormWithEvents).then((fixture) => {
el = fixture.debugElement.nativeElement;
input = fixture.debugElement.query(By.css("input")).nativeElement;
form = fixture.debugElement.query(By.css("form")).nativeElement;
fixture.detectChanges();
return fixture;
});
}
it('displays errors with no sku', injectAsync([TestComponentBuilder], (tcb) => {
return createComponent(tcb).then((fixture) => {
input.value = '';
dispatchEvent(input, 'input');
fixture.detectChanges();
// no value on sku field, all error messages are displayed
let msgs = el.querySelectorAll('.ui.error.message');
expect(msgs[0]).toHaveText('SKU is invalid');
expect(msgs[1]).toHaveText('SKU is required');
});
}));
it('displays no errors when sku has a value', injectAsync([TestComponentBuilder], (tcb) => {
return createComponent(tcb).then((fixture) => {
input.value = 'XYZ';
dispatchEvent(input, 'input');
fixture.detectChanges();
let msgs = el.querySelectorAll('.ui.error.message');
expect(msgs.length).toEqual(0);
});
}));
it('handles sku value changes', inject([TestComponentBuilder],
fakeAsync((tcb) => {
createComponent(tcb).then((fixture) => {
input.value = 'XYZ';
dispatchEvent(input, 'input');
tick();
expect(fakeConsole._logs).toContain('sku changed to: XYZ');
});
})
));
it('handles form changes', inject([TestComponentBuilder],
fakeAsync((tcb) => {
createComponent(tcb).then((fixture) => {
input.value = 'XYZ';
dispatchEvent(input, 'input');
tick();
expect(fakeConsole._logs).toContain('form changed to: [object Object]');
});
})
));
it('handles form submission', inject([TestComponentBuilder],
fakeAsync((tcb) => {
createComponent(tcb).then((fixture) => {
input.value = 'ABC';
dispatchEvent(input, 'input');
tick();
fixture.detectChanges();
dispatchEvent(form, 'submit');
tick();
expect(fakeConsole._logs).toContain('you submitted value: ABC');
});
})
));
});
示例4: beforeEachProviders
import { describe, it, expect, beforeEachProviders, inject } from 'angular2/testing'
import { ChatApp } from '../app/chatapp'
beforeEachProviders(() => [ChatApp]);
describe('ChatApp: Chatapp', () => {
});
示例5: beforeEachProviders
import {describe, it, expect, beforeEachProviders, inject} from 'angular2/testing';
import {AppComponent} from './app.ts';
beforeEachProviders(() => [AppComponent]);
describe('Houston App', () => {
it('should initiate the data service upon bootstrap',
inject([AppComponent], (app: AppComponent) => {
expect(app['_dataService'].developers$).toBeDefined();
}));
});
示例6: describe
describe('MarkPipes Pipe', () => {
beforeEachProviders(() => [MarkPipe]);
});
示例7: describe
describe('RestService', () => {
beforeEachProviders(() => [
HTTP_PROVIDERS,
provide(XHRBackend, { useClass: MockBackend }),
RestService
]);
describe('Travel', () => {
it('get()', inject([RestService, XHRBackend], (rest: RestService, mockBackend: MockBackend) => {
mockBackend.connections.subscribe(c => {
if (c.request.url === `${BASE_API_URL}/api/explore/partyTypes` && c.request.method === 0) {
let res = new Response(
new ResponseOptions(
{
body: {
"results": [{
"category_id": "123",
"question_id": "123",
"image_url": "http://test",
"description": "I am a banana",
"goodFor": "Me",
"suggestions": "Good for you",
"self": "http://another-link",
"text": "This is all me"
}]
},
status: 200
}
)
);
c.mockRespond(res);
}
})
rest.traveling.get((err, response: Array<Travel>) => {
expect(response.length).toBe(1);
expect(response[0].category_id).toBeDefined();
expect(response[0].category_id).toBe("123");
});
}));
});
describe('Questions', () => {
var rest: RestService;
var mockBackend: MockBackend;
let before = (_rest, _mockBackend, cb) => {
rest = _rest;
mockBackend = _mockBackend;
mockBackend.connections.subscribe(c => {
let testPayload = {
"category_id": "123",
"question_id": "234",
"image_url": "test234",
"description": "this is a test",
"goodFor": "everyone",
"suggestions": "awesome stuff",
"self": "http://test",
"text": "this is some text"
};
if (c.request.url === `${BASE_API_URL}/api/explore/categories/123/questions` && c.request.method === 0) {
send200({
"results": [
testPayload
]
}, c);
return;
}
testPayload.question_id = "345";
if (c.request.url === `${BASE_API_URL}/api/explore/questions/345` && c.request.method === 0) {
send200({
"results": [
testPayload
]
}, c);
return;
}
});
cb();
}
it('get()', beforeHack(before, () => {
rest.questions.get(123, (err, questions: Array<Question>) => {
expect(questions.length).toBe(1);
expect(questions[0].question_id).toBe("234");
});
}));
it('getById()', beforeHack(before, () => {
rest.questions.get(345, (err, question: Question) => {
expect(question.question_id).toBe("345");
});
}));
});
//.........这里部分代码省略.........
示例8: beforeEachProviders
import {describe, it, expect, beforeEachProviders, inject} from 'angular2/testing';
import {GestaoLivreApp} from '../app/gestaolivre';
beforeEachProviders(() => [GestaoLivreApp]);
/*
describe('App: Gestaolivre', () => {
it('should have the `defaultMeaning` as 42', inject([GestaoLivreApp], (app: GestaoLivreApp) => {
expect(app.defaultMeaning).toBe(42);
}));
describe('#meaningOfLife', () => {
it('should get the meaning of life', inject([GestaoLivreApp], (app: GestaoLivreApp) => {
expect(app.meaningOfLife()).toBe('The meaning of life is 42');
expect(app.meaningOfLife(22)).toBe('The meaning of life is 22');
}));
});
});
*/
示例9: beforeEachProviders
import {describe, it, expect, beforeEachProviders, inject} from 'angular2/testing';
import {MainApp} from '../app/main';
beforeEachProviders(() => [MainApp]);
示例10: beforeEachProviders
import {describe, it, expect, beforeEachProviders, inject} from 'angular2/testing';
import {TourApp} from '../app/tour';
beforeEachProviders(() => [TourApp]);
describe('App: Tour', () => {
it('should have the `defaultMeaning` as 42', inject([TourApp], (app) => {
expect(app.defaultMeaning).toBe(42);
}));
describe('#meaningOfLife', () => {
it('should get the meaning of life', inject([TourApp], (app) => {
expect(app.meaningOfLife()).toBe('The meaning of life is 42');
expect(app.meaningOfLife(22)).toBe('The meaning of life is 22');
}));
});
});