问题描述
我有一项服务,我想在 Angular 4 typescript jasmine 中进行单元测试.
I have a service I want to unit test in angular 4 typescript jasmine.
现在, http
正在执行 post
,它返回一个身份,但是......它没有发送任何东西.
Now, the http
is doing a post
, and it returns an identity, however.. it is not sending anything.
我只想拥有良好的代码覆盖率,但我不明白如何完全完成这个模拟语句.
I want to just have good code coverage but i don't understand how to quite complete this mocking statement.
这是我的服务文件中http post的方法
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());
}
然后是 SPEC FILE ,我没有得到真正测试的内容,我想假装我从服务 http 帖子中收到了一个数字,响应应该类似于 000000014
Then the SPEC FILE , which i don't get what to really test, i suppose faking that i received a number back from the service http post, the response should be something like 000000014
规格
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('add session ', () => {
// how to test, what to test?
// response , is a number? how to mock/fake this?
})
})
})
推荐答案
为了实现你想要的,你需要的mock是一个简单的函数,它返回的和POST正常的一样;另一件事是你的测试不应该真正到达服务器,所以你需要这样的东西(你可能需要添加其他依赖项):
In order to achieve what you want, the mock you need is a simple function that returns the same as the POST would do normally; another thing is your test should not hit the server for real, so you would need something like this (you might need to add other dependencies):
import { HttpModule } from '@angular/http';
import { TrackerFormService } from './tracker-form.service'
import { Observable } from 'rxjs/Observable'
describe('TrackerFormService', () => {
// Mock the service like this and add all the functions you have in this fashion
let trackerFormService: TrackerFormService,
mockService = {
addSession: jasmine.createSpy('addSession').and.returnValue(Observable.of('your session object mock goes here'))
};
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [{
provide: TrackerFormService,
useValue: mockService
}]
});
});
// Do this trick to inject the service every time, and just use `service` in your tests
beforeEach(inject([TrackerFormService], (trackerFormService) => {
service = trackerFormService;
}));
describe('addSession', () => {
it('add session ', () => {
let fakeResponse = null;
// Call the service function and subscribe to it to catch the fake response coming from the mock.
service.addSession().subscribe((value) => {
// in here value will be whatever you put as returnValue (remember to keep the observable.of())
fakeResponse = value;
});
// expects as in any test.
expect(fakeResponse).toBeDefined();
expect(fakeResponse).toBe('your session object mock goes here');
});
});
});
这篇关于带有 jasmine/karma 的 Angular 4 单元测试和 http post mocking - 如何修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!