2016-10-02 58 views
0

我有以下代码: (beforeEach)茉莉花规范并没有期待

spyOn(HttpService, 'post').and.callFake(function (url, paging, targetSpinner) { 
     return $q.when(_fakeServerResponse); 
    }); 

测试用例:

it('should compare size', function() { 
    service.get({},'','').then(function (serviceResponse) { 
     expect(serviceResponse.x).toEqual(_fakeServerResponse.x); 

和get方法:

return httpService.post(apiUrls).then(postComplete) 

我的问题正如标题中提到的那样:为什么茉莉花说没有预期。

服务本身在使用不运行测试:

myService.get(data, param1, param2).then(getComplete); 

我也补充一点,运行规范情况下,getComplete不会被调用,这是问题的根源,因为我看到它(但我不知道为什么它不会被调用)。

谢谢

回答

1

$ q是异步的。返回的承诺只能在下一个范围摘要中解决。您的测试而应是这样的:

it('should compare size', inject(function($rootScope) { 
    var actualX; 
    service.get({},'','').then(function(serviceResponse) { 
    actualX = serviceResponse.x; 
    }; 

    $rootScope.$apply(); // that will actually resolve the promise 

    expect(actualX).toEqual(_fakeServerResponse.x); 
})); 
+0

谢谢你,我改变了它,我得到的回应回来,但是,该postComplete方法返回后,我得到的错误:错误:意外的请求:POST/API/V1 /用户/索引 没有更多的预期请求,这意味着测试中的then子句永远不会进入。怎么会这样?所有的请求应该由假的应该取代它不应该? –

+0

在看不到您的代码时很难解释原因。 –