我创建了一个模拟服务,它就像我的真实服务一样,但为我的单元测试返回一组定义的值。如果我将该模块添加到我的应用程序。它拦截了调用并返回我在模拟中的内容,就像它假设的那样。
但是当我将这个模块添加到我的茉莉花测试中时,我似乎无法获得一个值
beforeEach(module('VideoConference'));//my app
beforeEach(module('lookupServiceMock')); //the mock service
beforeEach(inject(function(Mlb) { // Mlb is a factory that submits requests
mlb = Mlb;
spyOn(mlb, 'submitRequest').and.callThrough();
}));
...
it("Should Create a new conference", function () {
childScope = $scope.$new();
vm = $controller('TitleAndDateCtrl', { $scope: childScope });
childScope.requiredFieldsMet = function () { return true }
vm.conference.Title = "Test";
vm.createConference();
expect(mlb.submitRequest).toHaveBeenCalled();//this expectation comes back true
});
it("Should have returned a value", function() {
var id = mlb.get("id");//at this point expect that the id has been set
expect(id).toBe(123);
});
当我单步执行时,控制器会转到$ http.post(...)的位置,但它不会触发我的模拟或返回值。它不会成功发布。 任何想法如何让它击中我的模拟,返回值
答案 0 :(得分:0)
您应该将httpMock = $httpBackend;
添加到beforeEach
。
然后在你的测试中:
it("Should have returned a value", function() {
httpMock.expectGET('PATH_TO_YOUR_HTTP_GET').respond({id: 123});
var id = mlb.get("id");//at this point expect that the id has been set
httpMock.flush();
expect(id).toBe(123);
});
我不知道你的回应应该如何,所以你应该改变它。