admin管理员组文章数量:1355564
I am new to Jasmine tests and I am trying to write unit test for change event,which requires event as mock value in the method argument and I am unable to achieve it. this is what I have tried
it('sample test', () => {
const piled = fixture1.debugElement;
const event = {
preventDefault: jasmine.createSpy(),
srcElement: jasmine.createSpy()
};
spyOn(ponent1, 'onChange');
const select = piled.query(By.css('#elect-menu')).nativeElement;
select.value = select.options[1].value;
select.dispatchEvent(new Event('change'));
fixture1.detectChanges();
expect(ponent1.onChange).toHaveBeenCalled();`
my html code looks like this
<select id="select-menu" (change)="onChange($event)" (dblclick)="onChange($event)">
<option value="default">some value</option>
<option *ngFor="let line of lines" [value]="line.id" >{{line.name}}</option>
</select>
my ponent method which will be called on change onChange($event) {
const selected = parseInt($event.target.value);
switch (selected) {
case 1: {
//some logic
break;
}
}
I want to write a test case to test positive and negative flows in case 1.
I am new to Jasmine tests and I am trying to write unit test for change event,which requires event as mock value in the method argument and I am unable to achieve it. this is what I have tried
it('sample test', () => {
const piled = fixture1.debugElement;
const event = {
preventDefault: jasmine.createSpy(),
srcElement: jasmine.createSpy()
};
spyOn(ponent1, 'onChange');
const select = piled.query(By.css('#elect-menu')).nativeElement;
select.value = select.options[1].value;
select.dispatchEvent(new Event('change'));
fixture1.detectChanges();
expect(ponent1.onChange).toHaveBeenCalled();`
my html code looks like this
<select id="select-menu" (change)="onChange($event)" (dblclick)="onChange($event)">
<option value="default">some value</option>
<option *ngFor="let line of lines" [value]="line.id" >{{line.name}}</option>
</select>
my ponent method which will be called on change onChange($event) {
const selected = parseInt($event.target.value);
switch (selected) {
case 1: {
//some logic
break;
}
}
I want to write a test case to test positive and negative flows in case 1.
Share Improve this question edited Dec 3, 2018 at 6:16 Curious Coder asked Dec 2, 2018 at 16:41 Curious CoderCurious Coder 431 gold badge1 silver badge4 bronze badges1 Answer
Reset to default 3You've got a couple things going on here. First, there's no need to use spyOn for a method inside the ponent you're testing. Instead, you should use an expect() to check if the onChange() method did what it was supposed to. For example:
onChange($event) {
const selected = parseInt($event.target.value);
switch (selected) {
case 1:
this.testValue = 1; // Set some variable based on the selected value
break;
...
}
}
it('sample test', () => {
const piled = fixture1.debugElement;
const select = piled.query(By.css('#select-menu')).nativeElement;
select.value = select.options[1].value;
select.dispatchEvent(new Event('change'));
fixture1.detectChanges();
expect(ponent.testValue).toBe(1); // Check to see if the variable is correctly set
}
Second, you have a typo here: const select = piled.query(By.css('#elect-menu')).nativeElement;
- should be '#select-menu'
;
If you really just want to use a spy on your method, the correct syntax is:
let methodSpy = spyOn(ponent1, 'onChange').and.callThrough();
// Do something that triggers the method
expect(methodSpy).toHaveBeenCalled();
本文标签: javascriptHow to mock an event and pass it to a method in jasmine testsStack Overflow
版权声明:本文标题:javascript - How to mock an event and pass it to a method in jasmine tests? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743978797a2570987.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论