samedi 10 juin 2017

Jasmine testing: test an angular controller method

I'm using Jasmine to test an Angular controller which has a method that makes a call to a service which return a promise.

After call the controller method "$scope.CustomerTest" I expect to get the service call "marketingService.getTitleSuggested" and the $scope.TitlesTest populated.

Here is my code, can I get an example of how to do it please?

//Module
var app = angular.module("marketingApp", []);

//Service
app.service("marketingService", function ($http, $q) {

var service = this;

 service.getTitleSuggested = function (filter) {

    var deferred = $q.defer();
    var dataJson = JSON.stringify({ 'filter': filter });

    $http({
        url: "/MarketingCustomers/GetTitleSuggested",
        method: "POST",
        data: dataJson
    })
    .success(function (data) {
        // The promise is resolved once the HTTP call is successful.
        deferred.resolve(data);
    })
    .error(function () {
        // The promise is rejected if there is an error with the HTTP call.
        deferred.reject();
    });

    return deferred.promise;
 };

});

//Controller
app.controller("customerController", function($scope, $q, marketingService) {

$scope.CustomerTest = function(value) {
marketingService.getTitleSuggested(value)
  .then(function(response) {
    $scope.TitlesTest = response.data;
  });
}

});

Aucun commentaire:

Enregistrer un commentaire