[英]Mocked Angular navigateByUrl still running page reload
I am trying to write a test for a button click which triggers a navigateByUrl function call with some params.我正在尝试为按钮单击编写一个测试,该测试会触发带有一些参数的 navigateByUrl function 调用。
I have mocked Router and provided to the testing module as such我已经模拟了路由器并提供给测试模块
let mockRouter = {
navigateByUrl: jasmine.createSpy("navigateByUrl").and.callFake(() => {
console.log("fake is called");
}),
};
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpClientModule],
declarations: [RecoverPasswordComponent],
providers: [{ provide: Router, useValue: mockRouter }],
}).compileComponents();
}));
when i click the button and navigateByUrl is triggered, i am getting the console as written but i am also getting a error as Some of your tests did a full page reload!
当我单击按钮并触发 navigateByUrl 时,我得到了控制台,但我也收到了错误,因为Some of your tests did a full page reload!
which means that actual navigateByUrl is triggered.这意味着触发了实际的 navigateByUrl。
I am not able to get an idea of, if the function is spied than why the actual function call is triggered?我不知道,如果 function 被监视而不是为什么实际的 function 调用被触发?
I Finally figured out what was causing this, So basically i had a form in which my button was stated.我终于弄清楚了是什么原因造成的,所以基本上我有一个表格,其中说明了我的按钮。 as the form was not binded with ng-submit callback so on clicking the button the default event for a form button was triggered which reloaded the test suite.由于表单未与 ng-submit 回调绑定,因此单击按钮时会触发表单按钮的默认事件,从而重新加载测试套件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.