Last active
November 25, 2018 04:19
-
-
Save manivelarjunan/af7431e83d3f12d89d01b148a05b8791 to your computer and use it in GitHub Desktop.
setup function in spec file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { TestBed, async } from '@angular/core/testing'; | |
import { AppComponent } from './app.component'; | |
describe('App component', () => { | |
beforeEach(async(() => { | |
// The TestBed is the most important of the Angular testing utilities. | |
// The TestBed creates a dynamically-constructed Angular test module that emulates an Angular @NgModule. | |
// The TestBed.configureTestingModule() method takes a metadata object that can have most of the properties of an @NgModule. | |
TestBed.configureTestingModule({ | |
declarations: [AppComponent] | |
}).compileComponents(); | |
})); | |
describe(':', () => { | |
// Begin by putting re-usable, preparatory code in a setup function instead of beforeEach(). | |
// The setup() function returns an object literal with the variables, such as app, that a test might reference. | |
// You don't define semi-global variables (e.g., let app,fixture ) in the body of the describe(). | |
// Then each test invokes setup() in its first line, before continuing with steps that | |
// manipulate the test subject and assert expectations. | |
// Learn about destructuring assignment syntax is a JavaScript expression | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment | |
function setup() { | |
const fixture = TestBed.createComponent(AppComponent); | |
const app = fixture.debugElement.componentInstance; | |
return { fixture, app }; | |
} | |
it('should create the app', async(() => { | |
const { app } = setup(); | |
expect(app).toBeTruthy(); | |
})); | |
it('should have title as \'Angular7-unit-testing!\'', async(() => { | |
const { app } = setup(); | |
expect(app.title).toBe('Angular7-unit-testing!'); | |
})); | |
it('should have h1 tag as \'Welcome Angulat7-unit-testing!\'', async(() => { | |
const { app, fixture } = setup(); | |
fixture.detectChanges(); | |
const compile = fixture.debugElement.nativeElement; | |
const h1tag = compile.querySelector('h1'); | |
expect(h1tag.textContent).toBe(' Welcome to Angular7-unit-testing!! '); | |
})); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment