【问题标题】:Jasmine spec has no expectation茉莉花规格没有期望
【发布时间】:2017-02-10 09:03:19
【问题描述】:

我有以下代码: (每个之前)

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,这是我看到的问题的根源(但我不知道为什么它没有被调用)。

谢谢

【问题讨论】:

    标签: angularjs jasmine


    【解决方案1】:

    $q 是异步的。返回的 Promise 只会在下一个范围摘要中解析。您的测试应该看起来像:

    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);
    }));
    

    【讨论】:

    • 谢谢,我改变了它,我得到了响应,但是,在 postComplete 方法返回后,我得到错误:错误:意外请求:POST /api/v1/User/Index 没有更多请求这意味着测试中的 then 子句永远不会进入。怎么会这样?所有请求都应该被假请求替换,不是吗?
    • 不看你的代码很难解释原因。
    猜你喜欢
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 2014-12-12
    相关资源
    最近更新 更多