Reputation: 15303
In my test spec, I would like to insure 2 stuffs.
1. dummy data maching with model and get response.
2. find the `api` called with correct url.
I am trying those, but getting error as:
SubsystemServiceService › get susbsytem collection › should call get the subsytem collection
as well i would like to update my spec file to insure the both my requirement. any one please help me out here?
here is my spec.ts file:
import { TestBed } from '@angular/core/testing';
import { HttpClient } from '@angular/common/http';
import { cold } from 'jasmine-marbles';
import { ModelSubSystem } from './../models/model.subSystem';
import { SubsystemService } from './subsystem-service';
describe('SubsystemServiceService', () => {
let service: SubsystemService;
let http: HttpClient;
const data1 = {
Id: 0,
Name: 'subsystem1',
IsDeletePossible: true,
CreatedBy: '',
CreatedDate: new Date(),
UpdatedBy: '',
UpdatedDate: new Date(),
UpdatedByName: '',
CreatedByName: ''
} as ModelSubSystem;
const data2 = {
Id: 0,
Name: 'subsystem2',
IsDeletePossible: true,
CreatedBy: '',
CreatedDate: new Date(),
UpdatedBy: '',
UpdatedDate: new Date(),
UpdatedByName: '',
CreatedByName: ''
} as ModelSubSystem;
const subsystems = [data1, data2];
beforeEach(() => {
TestBed.configureTestingModule({
providers: [{ provide: HttpClient, useValue: { get: jest.fn() } }]
});
service = TestBed.get(SubsystemService);
http = TestBed.get(HttpClient);
});
describe('get susbsytem collection', () => {
it('should call get the subsytem collection', () => {
const response = cold('(-a|)', { a: subsystems });
http.get = jest.fn(() => response);
expect(http.get).toHaveBeenCalledWith(`https://ewsanedevaoscmsapi01-as.websites.net/api/SubSystem`);
});
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
Upvotes: 1
Views: 1618
Reputation: 26362
Where is your actual action? In a test there should be three things
describe('get susbsytem collection', () => {
it('should call get the subsytem collection', () => {
const response = cold('(-a|)', { a: subsystems });
http.get = jest.fn(() => response);
So you need to perform some action and then check the outcome. Something like
service.get();
expect(http.get).toHaveBeenCalledWith(`https://ewsanedevaoscmsapi01-as.websites.net/api/SubSystem`);
});
});
Imagine that you want to test the following method of a service:
getSubsystems() {
// Some code lead
// ...
// One path leads to get
if (some condition)
return http.get('url');
}
In order to test the scenario
it('should call the url when some condition is fulfilled')
you need to:
1. Setup the conditions so that 'some condition' is fulfilled
2. Create the mock response and the spy const response = cold('(-a|)', { a: subsystems });
3. Create a service instance and call the code to test getSubsystems()
4. Assert that after you called the code, the http.get has been called with the expected argument.
If you don't do the 3, the test framework does not know which unit (method) to execute and so executes nothing.
Upvotes: 2