茉莉花间谍对于Angularjs + RequireJs控制器失败

时间:2019-02-20 09:46:54

标签: angularjs jasmine requirejs

我正在尝试使用以下服务来测试AngularJS控制器:

define(['app', 'angular', 'angular-mocks'], function(app, angular, angularMocks) {
 describe("Products Controller", function() {
        var $controller, ProductService, createController, scope;

        beforeEach(function() {
          module('app');

          // Provide will help us create fake implementations for our dependencies
          module(function($provide) {

            // Fake StoreService Implementation returning a promise
            $provide.value('ProductService', {
                getProducts: function() {
                return { 
                  then: function(callback) {return callback([{ some: "thing", hoursInfo: {isOpen: true}}]);}
                };
              }
            });
            return null;
          });
        });

        beforeEach(function() {

          inject(function($controller, $rootScope, _ProductService_) {
            scope = $rootScope.$new();
            ProductService = _ProductService_;
            createController = function(params) {
              return $controller("ProductsController", {
                $scope: scope
              });
            };
          });
        });

        it("should call the store service to retrieve the store list", function() {         
          spyOn(ProductService, 'getProducts').and.callThrough();         
          createController();
          expect(ProductService.getProducts).toHaveBeenCalled();
        });
      });
    });

但是我收到错误消息,提示为“错误:预期的间谍getProducts已被调用。”

尽管茉莉花spyon被调用,但是测试用例失败了。

0 个答案:

没有答案