What Is Test Function?
After installing everything as per your project requirements, CREATE your project.
The following Steps –
· ng new YourTestProject
· ng install
· ng serve/ng test
If you are going to development then type “ng server” command and if you want to test your project, you should type “ng test” command. After type “ng test” command and press enter. It’s taking some time to installing everything in your project for a test.
Test functions–
1. describe – Test suit (just a function)
2. it - The spec or test
3. expect - Expected outcome.
Triple Rule of Testing –
1. Arrange - Create and Initialize the Components
2. Act - Invoke the Methods/Functions of Components
3. Assert - Assert the expected outcome/behavior
Best Practices - The quick list of best practices.
1. Use beforeEach() to Initialize the context for your tests.
2. Make sure the string descriptions you put in describe () and it () make sense as output
3. Use after () and afterEach () to clean-up your tests if there is any state that may bleed over.
4. If any one test is over 15 lines of code, you may need to refactor the test
Example -
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
//describe – Test suit (just a function)
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [AppComponent]
}).compileComponents();
}));
//it - The spec or test
it('should have hello property', function() {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
//expect – this is expected outcome.
expect(app.hello).toBe('Hello, Anil!');
});
});