用Angular js编写的方法的测试用例

时间:2016-01-21 07:05:32

标签: angularjs unit-testing methods testcase

我是angularjs的绝对新手。我在角度js中写了下面的函数。

$scope.ClickOnceGetProfilesByEnv = function () {

        $scope.PTPProfileList = undefined;
        $scope.PTPperson = undefined;

        var params = {
            Environment: $scope.PTPEnvironment,
            SearchCriteria: null,
            CatalogName: null,
            catalogID: null,
            ProfileId: null,
            IdentityId: null

        };
        ChannelService.DoAPICall(ChannelApp.configData.baseDirectory + '/api/TestHarness/GetProfiles/', 'POST', params)
                 .success(function (data) {
                     if (data) {
                         $scope.PTPProfiles = data;
                     } else {
                         $scope.PTPProfiles = {};
                     }
                 }).error(function (data) {
                     $scope.PTPProfiles = {};
                 });

    };

    $scope.ClickOnceGetIdentities = function () {
        $scope.PTPPerson = undefined;
        var profileId = $scope.PTPProfileList.ProfileId;
        var params = {
            Environment: $scope.PTPEnvironment,
            SearchCriteria: null,
            CatalogName: null,
            catalogID: null,
            ProfileId: profileId,
            IdentityId: null
        };

        ChannelService.DoAPICall(ChannelApp.configData.baseDirectory + '/api/TestHarness/GetIdentities/', 'POST', params)
                  .success(function (data) {
                      if (data) {
                          $scope.PTPIdentity = undefined;
                          $scope.PTPIdentitys = data;
                      } else {
                          $scope.PTPIdentity = {};
                      }
                  }).error(function (data) {
                      $scope.PTPIdentity = {};
                  });
    }

    $scope.PublishToProcessor = function () {
        $scope.setLoading(true);
        var params = {};
        params = {
            Environment: $scope.parNewEnvironment,
            ProfileId: $scope.PTPProfileList.ProfileId,
            IdentityId: $scope.PTPPerson.Identity_ID,
            CatalogType: $scope.parCatalogType
        };
        $http.post(ChannelApp.configData.baseDirectory + "/Catalog/PublishToProcessor", params).success(function (data) {

            $scope.setLoading(false);
        }).error(function (error) {
            alert('Error while publishing to processor');
            $scope.setLoading(false);
        });
    };

对于上述方法,我编写了以下测试方法。

it('Should Get Profiles based on Environment', function() {
        cataloglist.scope.$apply();
        cataloglist.scope.ClickOnceGetProfilesByEnv();
        $timeout.flush();
        expect(cataloglist.scope.PTPProfiles);
    });

    it('Should Get Identities by Environment', function() {
        cataloglist.scope.$apply();
        cataloglist.scope.ClickOnceGetProfilesByEnv();
        cataloglist.scope.ClickOnceGetIdentities();
        $timeout.flush();
    });

    it('Should Publish to Processor', function () {
        cataloglist.scope.$apply();
        cataloglist.scope.ClickOnceGetProfilesByEnv();
        cataloglist.scope.ClickOnceGetIdentities();
        cataloglist.scope.PublishToProcessor();
        $timeout.flush();
    });

请告诉我,我是对还是错。如果需要进一步更改,请告诉我。

0 个答案:

没有答案