I'm having a problem creating the test file for the post-detail component. I always get this error:
Here is my post-detail.component.ts file:
Here is my post-detail.component.spec.ts file:
could you help me to solve this problem?
You're trying to read the name property of undefined (in the PostDetailComponent
, code row 24, name: this.user.name
), because when you declare your component, the field user
is not defined. That's why it tries to read the name
field of the user object.
You have a few ways to fix it:
name
property. You will have the next code on the 24th row: name: this.user?.name,
.component.user = {} as User;
. Then when your component tries to read the name property, you will have an empty object, but not undefined and you will have no errors.