简体   繁体   English

如何用茉莉花来测试这种角度方法

[英]How to test this angular method with jasmine

I have the following method in my angular controller. 我的角度控制器中有以下方法。 Note that the Upload.upload method is within the apply method. 请注意,Upload.upload方法在apply方法内。

$scope.apply = function () {
    $scope.validationErrors.reset();

    if ($scope.firstName.trim() === '') {
        $scope.validationErrors.firstName = 'First name is required.';
    }

    if ($scope.validationErrors.isValid()){

        Upload.upload({
            url: '/apply',
            fields: {
                'first_name': $scope.firstName,
                'last_name': $scope.lastName
            },
            file: $scope.localFile,
            fileName: $scope.filename
        }).success(function(data){
            if (data === 'successfully saved application'){
                $location.path('/thanks/' + $scope.jobId);
            }else{
                $scope.errorMessage = 'Woops, something went wrong with your application. ' + data;
            }
        });
    }
};

I can't figure out how to test the success portion of the method. 我不知道如何测试该方法的成功部分。 This is what I have so far but it throws an error on the scope.apply() line saying success is undefined. 到目前为止,这是我所拥有的,但是在scope.apply()行上引发了一个错误,指出成功是不确定的。

describe('apply calls the uploader upload method', function () {
    var upload;
    var returnMessage = 'successfully saved application';
    var deferred;

    beforeEach(inject(function ($rootScope, $controller, $httpBackend, $http, $q, Upload) {
        deferred = $q.defer();
        upload = Upload;
        httpBackend = $httpBackend;
        scope = $rootScope.$new();
        httpBackend.when('POST', '/apply').respond(returnMessage);
        $controller('ApplyController', {
            $scope: scope,
            $routeParams: { id: 1 },
            $http: $http,
            $q: $q,
            Upload: Upload,
            jobService: jobService
        });
        spyOn(upload, 'upload').and.returnValue(deferred);
    }));

    it('calls the Upload upload method', function () {
        scope.firstName = scope.lastName = scope.phoneNumber = scope.zipCode = scope.filename = 'test';
        scope.jobId = 1;
        scope.emailAddress = 'test@test.com';
        scope.apply();
        expect(upload.upload).toHaveBeenCalled();
    });
});

How can I test the success portion of this method? 如何测试此方法的成功部分? Thanks. 谢谢。

You will have to do $httpBackend.flush() to get the mocked http response. 您将必须执行$ httpBackend.flush()才能获得模拟的http响应。 Assuming everything else is correct, the below test should pass. 假设其他一切正确,则下面的测试应该通过。 See more information here 在这里查看更多信息

it('calls the Upload upload method', function () {
        scope.firstName = scope.lastName = scope.phoneNumber = scope.zipCode = scope.filename = 'test';
        scope.jobId = 1;
        scope.emailAddress = 'test@test.com';
        scope.apply();
        httpBackend.flush();
        expect(upload.upload).toHaveBeenCalled();
    });

Aside:- after each test verify that all of the requests defined via the expect api were made and also that there are no outstanding requests that need to be flushed. 撇开:-在每次测试之后,请验证是否已完成通过Expect api定义的所有请求,并且没有任何需要刷新的未完成请求。

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

EDIT: Spotted another issue. 编辑:发现了另一个问题。 When setting up the spy, you should return deferred.promise and not deferred. 设置间谍时,应返回deferred.promise而不是deferred。 Try this:- 尝试这个:-

spyOn(upload, 'upload').and.returnValue(deferred.promise);

If this also doesn't work, could you share a plunker for this? 如果这还是行不通的,您可以为此共用一个小插曲吗?

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

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