Angular 单元测试角度观测值
我是测试界的新手,我刚刚开始为现有的Angular 2代码编写单元测试。我有一个函数Angular 单元测试角度观测值,angular,jasmine,primeng,Angular,Jasmine,Primeng,我是测试界的新手,我刚刚开始为现有的Angular 2代码编写单元测试。我有一个函数confirmDelete,它返回Obserable,并在内部使用PrimeNG的ConfirmationService来获取用户对弹出窗口的反馈 函数的定义如下所示: confirmDelete(): Observable<boolean> { let confirmObservable = Observable.create((observer: Observer<boolean&g
confirmDelete
,它返回Obserable
,并在内部使用PrimeNG的ConfirmationService
来获取用户对弹出窗口的反馈
函数的定义如下所示:
confirmDelete(): Observable<boolean> {
let confirmObservable = Observable.create((observer: Observer<boolean>) => {
this.confirmationService.confirm({
header: 'Delete Confirmation',
message: 'Do you really want to delete this record?',
accept: () => {
observer.next(true);
observer.complete();
},
reject: () => {
observer.next(false);
observer.complete();
}
});
});
return confirmObservable;
}
我可能会模拟保留
accept
和reject
函数的引用。然后在测试中,您可以调用它们来检查它们是否发出正确的布尔值。差不多
class MockConfirmationService {
accept: Function;
reject: Function;
confirm(config: any) {
this.accept = config.accept;
this.reject = config.reject;
}
}
然后在您的测试中,只需调用accept
来测试是否发出true
,调用reject
来测试是否发出false
describe('serivce: ModalService', () => {
let modalService: ModalService;
let confirmService: MockConfirmationService;
beforeEach(() => {
confirmService = new MockConfirmationService();
TestBed.configureTestingModule({
providers: [
ModalService,
{ provide: ConfirmationService, useValue: confirmService }
]
});
modalService = TestBed.get(ModalService);
});
it('should return true when accepted', async(() => {
modalService.confirmDelete().subscribe(result => {
expect(result).toBe(true);
console.log('accepted test complete');
});
confirmService.accept();
}));
it('should return false when rejected', async(() => {
modalService.confirmDelete().subscribe(result => {
expect(result).toBe(false);
console.log('rejected test complete');
});
confirmService.reject();
}));
});
我是根据皮斯基勒的回答来做的。p-confirmDialog组件调用服务的subscribe,因此我必须添加requireConfirmationSource,这是我在priming源代码中找到的。我的答覆如下:
//
import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';
//
export class ConfirmationServiceMock {
public key: string = 'mock1';
public header: string = 'Delete Confirmation';
public icon: string = '';
public message: string = '';
// Call Accept to emulate a user accepting
public accept: Function;
// Call Reject to emulate a user rejecting
public reject: Function;
private requireConfirmationSource = new Subject<any>();
requireConfirmation$ = this.requireConfirmationSource.asObservable();
//
public confirm(config: any) {
console.log( 'In confirm service mock...' );
this.message = config.message;
this.accept = config.accept;
this.reject = config.reject;
console.log( this.message );
return this;
}
}
//
//
从'rxjs/Subject'导入{Subject};
从“rxjs/Observable”导入{Observable};
//
导出类确认服务锁{
公钥:字符串='mock1';
公共标题:字符串='删除确认';
公共图标:字符串=“”;
公共消息:字符串=“”;
//调用Accept以模拟用户接受
公众接受:功能;
//调用Reject以模拟拒绝的用户
公众排斥:功能;
私人需求确认来源=新主题();
requireConfirmation$=this.requireConfirmationSource.asObservable();
//
公共确认(配置:任意){
log('In confirm service mock…');
this.message=config.message;
this.accept=config.accept;
this.reject=config.reject;
console.log(this.message);
归还这个;
}
}
//
Thanks@peeskillet. 我尝试了上面的解决方案,但出现了一些类型不匹配错误。请看我上面编辑过的帖子。我应该可以。我测试过了,效果很好。请仔细看我的例子ModalService
它只是一个简单的类,将ConfirmationService
作为构造函数参数。我从你的帖子中复制并粘贴了stright方法如果你试图在不使用测试床的情况下进行测试(只是实例化测试中的服务,并将模拟传递给它的构造函数),那么你需要在尝试传递模拟之前“强制”模拟。但是使用测试床,我们不需要担心这些事情,因为类型检查只是编译时,我所说的强制转换是newmodalservice(confirmService)
或newmodalservice(confirmService as confirmissionservice)
//
import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';
//
export class ConfirmationServiceMock {
public key: string = 'mock1';
public header: string = 'Delete Confirmation';
public icon: string = '';
public message: string = '';
// Call Accept to emulate a user accepting
public accept: Function;
// Call Reject to emulate a user rejecting
public reject: Function;
private requireConfirmationSource = new Subject<any>();
requireConfirmation$ = this.requireConfirmationSource.asObservable();
//
public confirm(config: any) {
console.log( 'In confirm service mock...' );
this.message = config.message;
this.accept = config.accept;
this.reject = config.reject;
console.log( this.message );
return this;
}
}
//