I have a service that I need to unit test in Angular 4 using TypeScript and Jasmine.
The problem is with the http
where it needs to perform a post
request and get an identity in return, but for some reason, no data is being sent through.
My goal is to achieve high code coverage, however, I'm struggling with figuring out how to properly mock this statement.
This is the method responsible for the HTTP post in my service file:
addSession() {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post(this.url, JSON.stringify({}), options)
.map((response: Response) => response.json());
}
Now onto the SPEC FILE which confuses me on what exactly to test. Perhaps simulating receiving a number back from the service's HTTP post call? The response should look like 000000014
.
Spec
import { TrackerFormService } from './tracker-form.service'
import { Observable } from 'rxjs/Observable'
describe('TrackerFormService', () => {
let trackerFormService: TrackerFormService,
mockHttp;
beforeEach(() => {
mockHttp = jasmine.createSpyObj('mockHttp', ['get', 'post', 'put']
)
trackerFormService = new TrackerFormService(mockHttp);
});
describe('addSession', () => {
it('should add session ', () => {
// What should be tested here?
// Should the response be a number? How can we mock or fake this scenario?
})
})
})