无法使用Jasmine测试方法

时间:2016-05-04 21:10:10

标签: javascript angularjs unit-testing ionic-framework jasmine

如何在Jasmine中测试dialog.button的返回值?

angular.module('employer').factory('popFactory', function($window) {
  var dialog = {};
  dialog.button = function(){
    $window.location.href = 'tel:' + dialog.number;
    return $window.location.href;
  };
};

我在Jasmine尝试过以下单元测试但没有运气,

1)

it('should check for a value', function(){
  spyOn(dialog, 'button');
  expect(dialog.button).toEqual('tel:' + dialog.clientNumber);
});

控制台错误:Expected spy on button to equal 'tel:1234566789'.

2)

it('should check for a value', function(){
      spyOn(dialog, 'button');
      expect(dialog.button()).toEqual('tel:' + dialog.clientNumber);
    });

控制台错误:Expected undefined to equal 'tel:1234566789'.

我做错了什么?

display.test.js - Spec

我只发布了我的规范的一部分,因为它非常大并且包含大量测试用例。

describe('employer', function () {
  beforeEach(module('employer'));

  describe('factory: dialog', function() {
    var dialog = null;
    var rootScope, ionicPopup, windowMock, authenticationFactory, restfulService, brandingMock;

    beforeEach(inject(function(popFactory, $rootScope, $ionicPopup, $state, $window, authentication, restfulService, branding) {
      dialog    = popFactory;
      rootScope       = $rootScope;
      ionicPopup      = $ionicPopup;
      state           = $state;
      windowMock      = $window;
      authenticationFactory = authentication;
      restfulServiceMock         = restfulService;
      brandingMock  = branding;
    }));

    it('initializes', function () {
      expect(dialog.investigateNumber).toBe('');
      expect(dialog.alertingNumber).toBe('');
      expect(dialog.mirrorNumber).toBe(''); //All these tests pass
      expect(dialog.contactNumber).toBe('');
      expect(dialog.client).toBe(brandingMock.client);
      expect(dialog.currentCall).toEqual(dialog.alertNumber);
      expect(dialog.phone).toBe(brandingMock.phone);
    });

    it('logs', function () {
      spyOn(console, 'log');
      var err = 'test error';
      dialog.logError(err);
      expect(console.log).toHaveBeenCalledWith(err);
    });

    it('canceling passcode', function(){
      var title = 'title';
      spyOn(ionicPopup ,'show');
      dialog.confirmCancelPasscode(title);
      expect(ionicPopup.show).toHaveBeenCalled();
    });

    it('title-based alert', function(){
      var title = 'testing-title';
      spyOn(ionicPopup, 'show');
      dialog.showTitleAlert(title);
      expect(ionicPopup.show).toHaveBeenCalled()
    });

    it('generic alert', function(){
      var title = 'test-title';
      spyOn(ionicPopup, 'show');
      dialog.showGenericAlert(title);
      expect(ionicPopup.show).toHaveBeenCalled()
    });

    it('two-button alert', function(){
      var title = 'test-title';
      var message = 'test-message';
      var affirmativeText = 'test-affirm';
      var denyText = 'test-deny';
      var affirmation = function(){};
      var denyingAccess = affirmation;
    })
   };

0 个答案:

没有答案