我得到了一个角服务模样
function userService() {
// A library provide methods that not used $http service and not $.ajax either
this.lib = theLib;
// This one will invoke "api/v1/user"
this.getAllUser = function() {
return lib.getAll();
}
}
如何测试以下
it('should return all user', function() {
// How can I mock the returned data without using $http
userService.getAllUser();
})
这些功能它可能是库测试服务如果它使用$ http或$ ajax,那么我很容易使用$httpBackend
或模拟ajax的返回。还有另一种方法来模拟theLib
,但里面有很多方法,但我不认为这是个好主意。这种情况下的任何建议?
你可以尝试使用[XHR-模拟(https://github.com/jameslnewell/xhr-mock) – jcubic
谢谢@jcubic。我会尝试。 – tmhao2005
这真的取决于你正在瞄准的测试水平是什么。如果你对单元测试(即服务本身)感兴趣,那么你应该嘲笑图书馆,而不是通信提供商。 –