简体   繁体   English

茉莉花间谍没有被称为

[英]Jasmine spy not being called

I have created an angular service which I am testing without any issue, I then started trying to inject a dependency into the tests when I started having an issue. 我创建了一个我正在测试的角度服务,没有任何问题,然后当我开始遇到问题时,我开始尝试将依赖项注入测试中。

I want to make sure that a function in the dependency has been called but it's coming back undefined. 我想确保已调用依赖项中的一个函数,但该函数未定义返回。

Here's my service: 这是我的服务:

angular.module('enigmaApp.services', [])
    .factory('auth', function($window, $http, SS){
        var auth = {};

        auth.register = function(user){
            return $http.post('/register', user)
            .success(function(data){
                if(data.token){
                    SS.setObj('appToken', data.token);
                }
            });
        };

        return auth;
    });

My test: 我的测试:

describe('Auth Service Tests', function () {
  var $httpBackend, auth, defer, registerReqHandler, setObjSpy, SS, SSMock, user;

  beforeEach(module('enigmaApp'));

  beforeEach(function () {    
    // Create spies
    setObjSpy = jasmine.createSpy('setObj');

    SSMock = {
      setObj: setObjSpy
    }

    SS = SSMock;

    module(function ($provide) {
      $provide.value('SS', SSMock);
    });
  });

  beforeEach(inject(function (_$httpBackend_, $injector, $q) {
    $httpBackend = _$httpBackend_; 
    defer = $q.defer();
    registerReqHandler = $httpBackend.when('POST', '/register').respond(defer.promise);
    auth = $injector.get('auth');
  }));

  afterEach(function () {
    $httpBackend.flush();
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
  });

  describe('auth.register(user)', function () {
    beforeEach(function () {
      user = {
        email: 'bwayne@wayneenterprise.com',
        first_name: 'Bruce',
        last_name: 'Wyane',
        password: 'password123'
      };
    });

    it('should call SS.setObj on successful registration', function () {
      $httpBackend.expectPOST('/register').respond({ token: 'fakeToken' });
      auth.register(user);
      expect(SS.setObj).toHaveBeenCalled();
    });
  });

When I run the tests I get a failed test that says "Expected spy setObj to have been called." 运行测试时,我得到一个失败的测试,提示“预期的间谍setObj已被调用。” Any idea on what I'm doing wrong here? 对我在这里做错的任何想法吗? I thought I set up a mock for SS.setObj and provided it to the module, 我以为我为SS.setObj设置了一个模拟并将其提供给模块,

One problem you have is that it seems you aren't actually returning your auth object in your factory: 您遇到的一个问题是,看来您实际上并没有在工厂中返回auth对象:

angular.module('enigmaApp.services', [])
.factory('auth', function($window, $http, SS){
    var auth = {};

    auth.register = function(user){
        return $http.post('/register', user)
        .success(function(data){
            if(data.token){
                SS.setObj('appToken', data.token);
            }
        });
    };


    // need to return auth here...
    return auth;
});

update 更新

Move your flush call into your it block 将冲洗呼叫移至it区块

it('should call SS.setObj on successful registration', function () {

  $httpBackend
    .expectPOST('/register')
    .respond({ token: 'fakeToken' });

  auth.register(user);
  $httpBackend.flush();

  expect(SS.setObj).toHaveBeenCalled();
});

afterEach blocks are for clean up. afterEach块都需要清理。 Jasmine assumes your test is over by that point so your flush() call won't have any effect. Jasmine假设您的测试到那时就结束了,因此您的flush()调用不会有任何效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM