简体   繁体   English

使用Jasmine测试从viewChild调用另一个方法的方法

[英]Test a method which calls another method from the viewChild using Jasmine

I have this method that validates the component and it's calling out to this another component which is it's ViewChild . 我有这个方法验证组件,它正在调用另一个组件,它是ViewChild I am using the component using this.ViewChildComponent.someMethod(); 我正在使用this.ViewChildComponent.someMethod(); in the component which I am trying to test. 在我试图测试的组件中。 I tried to use the spyOn(viewChildComponent, 'someMethod').and.returnValue(true) . 我试图使用spyOn(viewChildComponent, 'someMethod').and.returnValue(true) But it says the this.ViewChildComponent.someMethod() is undefined . 但是它说this.ViewChildComponent.someMethod() is undefined I have all the dependencies such as services for the ViewChildComponent on the providers. 我有所有依赖项,例如提供程序上ViewChildComponent的服务。 I even went a step forward and made the call that the viewChildComponent makes to its service to decide someMethod(); 我甚至前进了一步,并调用了viewChildComponent对其服务进行调用以决定someMethod();

Any help would be awesome. 任何帮助都是极好的。

If you have for example 如果你有例如

class TestedComponent() {
     @ViewChild('ChildComp') public variableChildComponent: ChildComp;
}

In testing spec file declare child component and tested component: 在测试spec文件中声明子组件和测试组件:

beforeEach(() => {
  TestBed.configureTestingModule({
     declarations: [ TestedComponent, ChildComp]
  })
  fixture = TestBed.createComponent(TestedComponent);
  comp = fixture.componentInstance;
  spyOn(comp.variableChildComponent, 'someMethod').and.returnValue(true);
});

This should work. 这应该工作。

@DanielDrozzel answer may solve your problem, but essentially it is flawed and not a good practice. @DanielDrozzel的回答可以解决你的问题,但基本上它是有缺陷的,不是一个好的做法。 I would like to offer an alternative. 我想提供一个替代方案。 In Daniel's answer, when you use declarations: [ TestedComponent, ChildComp] you create a tight coupling in your unit tests between TestedComponent and ChildComp . 在Daniel的回答中,当您使用declarations: [ TestedComponent, ChildComp] ,您在TestedComponentChildComp之间的单元测试中创建紧耦合。 If the child component stops working your tests will fail, if it changes behaviour, the tests may fail. 如果子组件停止工作,您的测试将失败,如果它改变行为,测试可能会失败。

It's not always a bad thing, but it can sometimes bite you. 这并不总是坏事,但它有时会咬你。 It's better to use Component Stubs as mentioned in official Angular Docs https://angular.io/guide/testing#nested-component-tests or a very useful ng-mocks library. 最好使用官方Angular Docs https://angular.io/guide/testing#nested-component-tests中提到的Component Stubs或一个非常有用的ng-mocks库。 The third option mentioned in the Angular docks above is NO_ERRORS_SCHEMA but you can't use it if you want to test if child component's methods are called from parent. 上面的Angular NO_ERRORS_SCHEMA提到的第三个选项是NO_ERRORS_SCHEMA但是如果要测试是否从父级调用子组件的方法,则不能使用它。

Using ng-mocks library all you need to do is to swap 使用ng-mocks库你需要做的就是交换

declarations: [ TestedComponent, ChildComp] in Daniel's answer for declarations: [ TestedComponent, ChildComp]在Daniel的回答中

declarations: [ TestedComponent, MockComponent(ChildComp)]

and the parent component can be tested without creating a hard dependency on a child component. 并且可以在不创建对子组件的硬依赖性的情况下测试父组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM