[英]Unit Testing events in karma angular 2 application
I'm trying to unit test the method abc(Event) using karma.我正在尝试使用 karma 对方法 abc(Event) 进行单元测试。 I have a dropdown in my view page which has 4 options which up on change will trigger abc(Event) method.
我的视图页面中有一个下拉列表,其中有 4 个选项,更改后将触发 abc(Event) 方法。
Here is my view page:这是我的查看页面:
<div class="myClass">
<select (change)="abc($event)" id="my">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
</select>
my component.ts file contains the definition of method abc.我的 component.ts 文件包含方法 abc 的定义。
import {Component,OnInit} from '@angular/core';
import { LocaleService } from '../../../services/locale.service';
@Component({
selector: 'app-localeselector',
templateUrl: './localeselector.component.html',
styleUrls: ['./localeselector.component.scss']
})
export class LocaleselectorComponent implements OnInit{
private localeService: any;
private language;
constructor() { }
ngOnInit() { }
}
abc(ev: Event) {
//something...
}
}
How can I test method abc?我如何测试方法 abc? Also how to mock $event?
还有如何模拟 $event? Thanks in advance.
提前致谢。
Thanks, it worked!!!谢谢,成功了!!! :) I found the solution.
:) 我找到了解决方案。 Was able to mock the event by sending in an object.
能够通过发送对象来模拟事件。
it('should work', () => {
component.abc({ srcElement: { value: 'xyz' } });
expect(someMethod.getValue()).toEqual('xyz');
});
I also changed the abc method in my component.ts file to我还将 component.ts 文件中的 abc 方法更改为
abc($event){
//do something...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.