this is from official documentation https://angular.io/docs/ts/latest/guide/testing.html#!#component-fixture. So you can create new input object expectedHero and pass it to the component comp.hero = expectedHero
Also make sure to call fixture.detectChanges(); last, otherwise property will not be bound to component.
Working Example
// async beforeEach
beforeEach( async(() => {
TestBed.configureTestingModule({
declarations: [ DashboardHeroComponent ],
})
.compileComponents(); // compile template and css
}));
// synchronous beforeEach
beforeEach(() => {
fixture = TestBed.createComponent(DashboardHeroComponent);
comp = fixture.componentInstance;
heroEl = fixture.debugElement.query(By.css('.hero')); // find hero element
// pretend that it was wired to something that supplied a hero
expectedHero = new Hero(42, 'Test Name');
comp.hero = expectedHero;
fixture.detectChanges(); // trigger initial data binding
});
Answer from Vazgen Manukyan on Stack Overflowthis is from official documentation https://angular.io/docs/ts/latest/guide/testing.html#!#component-fixture. So you can create new input object expectedHero and pass it to the component comp.hero = expectedHero
Also make sure to call fixture.detectChanges(); last, otherwise property will not be bound to component.
Working Example
// async beforeEach
beforeEach( async(() => {
TestBed.configureTestingModule({
declarations: [ DashboardHeroComponent ],
})
.compileComponents(); // compile template and css
}));
// synchronous beforeEach
beforeEach(() => {
fixture = TestBed.createComponent(DashboardHeroComponent);
comp = fixture.componentInstance;
heroEl = fixture.debugElement.query(By.css('.hero')); // find hero element
// pretend that it was wired to something that supplied a hero
expectedHero = new Hero(42, 'Test Name');
comp.hero = expectedHero;
fixture.detectChanges(); // trigger initial data binding
});
If you use TestBed.configureTestingModule to compile your test component, here's another approach. It's basically the same as the accepted answer, but may be more similar to how angular-cli generates the specs. FWIW.
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { DebugElement } from '@angular/core';
describe('ProductThumbnail', () => {
let component: ProductThumbnail;
let fixture: ComponentFixture<TestComponentWrapper>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [
TestComponentWrapper,
ProductThumbnail
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
.compileComponents();
fixture = TestBed.createComponent(TestComponentWrapper);
component = fixture.debugElement.children[0].componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@Component({
selector: 'test-component-wrapper',
template: '<product-thumbnail [product]="product"></product-thumbnail>'
})
class TestComponentWrapper {
product = new Product()
}
unit testing angular component with @input - Stack Overflow
unit testing angular component with input.required - Stack Overflow
angular - Testing an @input() of a component - Stack Overflow
typescript - How to create component with @input on unit tests Angular? - Stack Overflow
Videos
To set inputs, you can use ComponentRef<T>.setInput method, which also works with signals:
fixture = TestBed.createComponent(NewComponent);
component = fixture.componentInstance;
fixture.componentRef.setInput('signal-input-name', 'value');
// e.g. setInput('myInput', 'test');
fixture.detectChanges();
This method takes the input name as a first parameter and the value to set signal input to as second parameter.
More in angular.dev documentation
EDIT: New API added in Angular 20.1 (commit 2e0c98bd3f)
In Angular 20.1 new API was added to testing package - inputBinding function (and the same for outputs), that allows you to bind component inputs (and outputs) to specified value or other signal.
With static value:
const fixture = TestBed.createComponent(NewComponent, {
bindings: [
inputBinding('signal-input-name', () => 'value')
]
});
fixture.detectChanges();
With reactive value:
const testedComponentInput = signal('value');
const fixture = TestBed.createComponent(NewComponent, {
bindings: [
inputBinding('signal-input-name', testedComponentInput)
]
});
// some tests...
testedComponentInput.set('new value!');
// other tests after changing value
As mentioned, new API has more methods, these are: outputBinding and twoWayBinding which allow you to set signals that will be bound to output or will handle two-way binding
You can use signalSetFn
import { SIGNAL, signalSetFn } from '@angular/core/primitives/signals';
fixture = TestBed.createComponent(SomeComponent);
component = fixture.componentInstance;
signalSetFn(component.required[SIGNAL], 'test');
fixture.detectChanges();