-1

I have a component with the following structure using this guidance

@Component({
  selector: 'app-component[required]',
  templateUrl: './component.component.html',
  styleUrls: ['./component.component.scss']
})
export class Component implements OnInit {

  @Input() required!: string;

  constructor() { }

  ngOnInit(): void {}
}
  

And when I execute its default test I've got the error component required at least required property and I have already tried what is they say here

here is the test I am running

describe('ComponentComponent', () => {
  let component: ComponentComponent;
  let fixture: ComponentFixture<ComponentComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [ ComponentComponent ]
    })
    .compileComponents();

    fixture = TestBed.createComponent(ComponentComponent);
    component = fixture.componentInstance;
    component.required = 'foo';
    fixture.detectChanges();
    component.required = 'foo';
  });

  it('should create', () => {
    fixture.detectChanges();
    component.required = 'foo';
    expect(component).toBeTruthy();
  });
});
Carepollo
  • 37
  • 1
  • 6
  • 1
    "and I have already tried what is they say here", where are you initializing `required` in your test? the link you posted does that, after `fixture.componentInstance`, you would do `component.required = 'something'` – Andres2142 Dec 27 '22 at 21:11
  • I skipped my trial doing that because the original thread already shows my point, the problem remains after doing that – Carepollo Dec 27 '22 at 21:19
  • 1
    Does this answer your question? [unit testing angular component with @input](https://stackoverflow.com/questions/63839249/unit-testing-angular-component-with-input) – possum Dec 28 '22 at 11:41
  • @possum No, It didn't, I have already tried that and didn't work, also I think the scenario is different, because is not only that I have an `Input` decorator but the fact that I have a selector that makes the property mandatory for the component. – Carepollo Dec 28 '22 at 13:47

1 Answers1

-1

try this

describe('ComponentComponent', () => {
  let component: ComponentComponent;
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      providers: [ ComponentComponent ]
    });
    component = TestBed.inject(ComponentComponent);
  });
  it('should create', () => {
    expect(component).toBeTruthy();
  });
});
  • This does not definitively answer the question and from what I can tell from reading the question, does nothing to address the issue. – possum Dec 28 '22 at 11:40