3

i'm trying to testing a component with ChangeDetectorRef

constructor(private cdRef: ChangeDetectorRef) {}

And this is the spec file

import {RTLateralMenuComponent} from "./RTLateralMenu.component";

describe('RTLateralMenuComponent', () => {
  let app: RTLateralMenuComponent;

  beforeEach(()=>{
    app = new RTLateralMenuComponent();
  });
});

new RTLateralMenuComponent obviously expect an argument, but i don't how it works.

3

1 Answer 1

4

You can mock it

const cdRefMock = {
  detectChanges: () => null
};

app = new RTLateralMenuComponent(cdRefMock);

You will have to implement every method used in your component : detectChanges being the most common one, I thought I would give it right away.

(PS : I assumed you don't use the testbed since you're creating an instance of your component)

Sign up to request clarification or add additional context in comments.

3 Comments

The following error appears: ''Property markforcheck is missing in type { detectChanges: () => null }".
Then add markforcheck in your mock. I told you to put every method you use in your mock.
No problem, I'm here for that !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.