对象的属性'$'不是函数jasmine

时间:2013-11-22 22:26:15

标签: javascript jquery angularjs jasmine

因此,当我尝试运行我的茉莉花测试时,我在每次测试时都会收到此错误:

TypeError: Property '$' of object [object Object] is not a function
at jasmine.Fixtures.cleanUp (http://localhost:8234/src/target/test-classes/frontend/thirdParty/js/com/github/velesin/jasmine-jquery/0.0.0/jasmine-jquery-0.0.0.js:117:3)
at null.<anonymous> (http://localhost:8234/src/target/test-classes/frontend/thirdParty/js/com/github/velesin/jasmine-jquery/0.0.0/jasmine-jquery-0.0.0.js:548:25)
at jasmine.Block.execute (http://localhost:8234/?:1152:19)
at jasmine.Queue.next_ (http://localhost:8234/?:2184:33)
at jasmine.Queue.start (http://localhost:8234/?:2137:10)
at jasmine.Spec.execute (http://localhost:8234/?:2464:16)
at jasmine.Queue.next_ (http://localhost:8234/?:2184:33)
at onComplete (http://localhost:8234/?:2180:20)
at jasmine.Spec.finish (http://localhost:8234/?:2438:7)
at null.onComplete (http://localhost:8234/?:2465:12)

我已经看过关于jquery在noConflict模式下运行的各种帖子和SO,我需要在整个代码中使用jQuery,但是我正在测试的代码里面没有任何$。

代码:

$provide.factory('corsHttpInterceptor', ['$q', '$window', '$exceptionHandler', '$rootScope', 'jq360', function($q, $window, $exceptionHandler, $rootScope, jq360){
            var corsHttpInterceptor,
                ieCorsTimeoutTime;

            function fixConfigForXdr(config)
            {
                if (config.method.toUpperCase() === "PUT")
                {
                    config.method = "POST";
                    if (angular.isDefined(config.params))
                    {
                        config.params._method = "put";
                    }
                    else
                    {
                        config.params = {_method: "put"};
                    }
                }
                else if (config.method.toUpperCase() === "DELETE")
                {
                    config.method = "GET";
                    if (angular.isDefined(config.params))
                    {
                        config.params._method = "delete";
                    }
                    else
                    {
                        config.params = {_method: "delete"};
                    }
                }
            }

            function getResponseDataForXdr(xdr)
            {
                var responseData = xdr.responseText;
                if ("application/json" === xdr.contentType)
                {
                    responseData = angular.fromJson(responseData);
                }
                return responseData;
            }

            function getIEUrl(config)
            {
                var url = config.url;
                if (angular.isDefined(config.params) && !angular.equals(config.params, {}))
                {
                    if (-1 === url.indexOf("?"))
                    {
                        url += "?";
                    }
                    else
                    {
                        url += "&";
                    }
                    url += jq360.param(config.params);
                }

                return url;
            }

            corsHttpInterceptor = {
                request: function(config){
                    var deferred,
                        promise,
                        xdr;

                    if ('withCredentials' in new $window.XMLHttpRequest())
                    {
                        return config;
                    }
                    else if (angular.isDefined($window.XDomainRequest))
                    {
                        config.method = angular.uppercase(config.method);
                        deferred = $q.defer();
                        //this promise already has the then function so don't need to add it
                        promise = deferred.promise;

                        try
                        {
                            xdr = new $window.XDomainRequest();
                        }
                        catch(e)
                        {
                            $exceptionHandler(new Error("CRITICAL: " + e.message), "new XDomainRequest()");
                        }
                        try
                        {
                            fixConfigForXdr(config);
                            xdr.open(config.method, getIEUrl(config));
                        }
                        catch(e)
                        {
                            $exceptionHandler(new Error("CRITICAL: " + e.message), "xdr.open");
                        }
                        xdr.onprogress = function() {}; //http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/30ef3add-767c-4436-b8a9-f1ca19b4812e/
                        xdr.ontimeout = function() {};
                        xdr.onload = function() {
                            try
                            {
                                var responseData = getResponseDataForXdr(xdr);
                                deferred.resolve({data: responseData, status: 200});
                                $rootScope.$apply();
                            }
                            catch(e)
                            {
                                $exceptionHandler(new Error("CRITICAL: " + e.message), "xdr.onload");
                            }
                        };
                        xdr.onerror = function() {
                            try
                            {
                                deferred.reject({data: "", status: 500});
                                $rootScope.$apply();
                            }
                            catch(e)
                            {
                                $exceptionHandler(new Error("CRITICAL: " + e.message), "xdr.onerror");
                            }
                        };
                        xdr.timeout = 0;

                        $window.setTimeout(function() { //IE CORS requests are inconsistent without the setTimeout. Reference: http://stackoverflow.com/questions/5250256/inconsistent-ajax-xdr-response-from-ie
                            try
                            {
                                if ("GET" === config.method)
                                {
                                    xdr.send();
                                }
                                else
                                {
                                    xdr.send(angular.toJson(config.data));
                                }
                            }
                            catch(e)
                            {
                                $exceptionHandler(new Error("CRITICAL: " + e.message), "xdr.send");
                            }
                        }, ieCorsTimeoutTime);//TEMPORARY way to dynamically set the timeout time for IE CORS requests

                        promise.success = function(fn) {
                            promise.then(function(response) {
                                fn(response.data, response.status);
                            });
                            return promise;
                        };

                        promise.error = function(fn) {
                            promise.then(null, function(response) {
                                fn(response.data, response.status);
                            });
                            return promise;
                        };

                        return promise;
                    }
                    else
                    {
                        throw new Error("Browser doesn't support needed functionality.");
                    }
                },
                response: function(response){
                    return response;
                },
                responseError: function(rejection){
                    return $q.reject(rejection);
                },
                ieCorsTimeoutTime: ieCorsTimeoutTime
            };

            return corsHttpInterceptor;
        }]);

试验:

'use strict';

var mockAppbaseModule;
describe('appbaseWithCors', function(){
    mockAppbaseModule = angular.module("appbase", []);
    beforeEach(module(function($provide, $exceptionHandlerProvider) {
        $provide.provider('jq360', function() {
            this.$get = function() {
                return $;
            };
        });
        $exceptionHandlerProvider.mode('log');
    }));
    beforeEach(module('appbaseWithCors'));

    describe("corsHttpInterceptor", function () {
        var successCallback = null;
        var errorCallback = null;
        var successResponse = {foo: 'blah'};
        var errorResponse = {errorCode: 123, appServer: 1};
        beforeEach(inject(function() {
            successCallback = jasmine.createSpy("successCallback");
            errorCallback = jasmine.createSpy("errorCallback");
        }));

        var appStatus;
        describe("Standard CORS", function () {
            beforeEach(inject(function($window){
                appStatus = {
                    appBaseUrl : "",
                    appServer: 1,
                    token: "TEST_TOKEN"
                };
                spyOn($window, "XMLHttpRequest").andReturn({withCredentials: 'foo'});
            }));
            it ("should call the error function when there is an error code in the response data", inject(function($http, $httpBackend) {
                $httpBackend.expectGET("TEST_URL").respond(403, errorResponse);
                var config = {method: "get", url:"TEST_URL"};
                $http(config).success(successCallback).error(errorCallback).then(successCallback, errorCallback);

                $httpBackend.flush();
                expect(successCallback).not.toHaveBeenCalled();
                expect(errorCallback).toHaveBeenCalledWith({data: errorResponse, status: 403, headers: jasmine.any(Function), config: jasmine.any(Object)});
            }));
        }));
    }));
}));

0 个答案:

没有答案