Angular JS Tutorial

Unit Testing Controllers and Services (using Jasmine)

अब हम AngularJS के testing world में कदम रखते हैं — जहाँ आप अपने code को automatically test कर सकते हैं कि वो सही तरीके से काम कर रहा है या नहीं।

✅ Unit Testing Controllers and Services in AngularJS (using Jasmine)

(बग पकड़ो, भरोसा बढ़ाओ, future-proof बनाओ!)


🧪 Unit Testing क्या है?

Unit Testing का मतलब है: Code के छोटे-छोटे हिस्सों (जैसे Controller या Service) को अलग-अलग test करना, बिना पूरे app को चलाए।

AngularJS में Unit Testing का सबसे popular framework है: Jasmine


🧰 Tools की ज़रूरत:

Toolकाम
JasmineTest लिखने और run करने के लिए
KarmaTests को browser में automatically चलाने के लिए
angular-mocks.jsAngularJS के modules/services को mock करने के लिए

यहां हम Jasmine + angular-mocks का basic use दिखाएंगे।


✅ Step-by-step Structure:

हम 2 चीजें test करेंगे:

  1. ✅ एक Controller
  2. ✅ एक Service

🧩 Example App Code:

1. Controller: MainCtrl

app.controller("MainCtrl", function($scope, UserService) {
  $scope.greeting = "Hello!";
  
  $scope.getUser = function() {
    $scope.user = UserService.getUser();
  };
});

2. Service: UserService

app.service("UserService", function() {
  this.getUser = function() {
    return { name: "Rahul", email: "rahul@example.com" };
  };
});

✅ Jasmine Test File: main.spec.js

describe("MainCtrl Unit Test", function() {

  var $controller, $rootScope, $scope, UserService;

  beforeEach(module("myApp")); // your app name here

  beforeEach(inject(function(_$controller_, _$rootScope_, _UserService_) {
    $controller = _$controller_;
    $rootScope = _$rootScope_;
    UserService = _UserService_;
    $scope = $rootScope.$new();

    $controller("MainCtrl", {
      $scope: $scope,
      UserService: UserService
    });
  }));

  it("should set default greeting", function() {
    expect($scope.greeting).toBe("Hello!");
  });

  it("should fetch user from UserService", function() {
    $scope.getUser();
    expect($scope.user).toEqual({
      name: "Rahul",
      email: "rahul@example.com"
    });
  });
});

✅ Jasmine Test for Service

describe("UserService Test", function() {

  var UserService;

  beforeEach(module("myApp"));

  beforeEach(inject(function(_UserService_) {
    UserService = _UserService_;
  }));

  it("should return correct user data", function() {
    var user = UserService.getUser();
    expect(user.name).toBe("Rahul");
    expect(user.email).toContain("@");
  });
});

✅ Folder Structure Suggestion

/app
  ├── main.js         (App code)
  ├── controllers.js
  ├── services.js
/tests
  ├── main.spec.js    (Jasmine test file)

🧠 Jasmine Keywords:

Keywordकाम
describe()Test group define करता है
it()एक specific test case
expect()actual vs expected values compare
beforeEach()हर test से पहले common setup
inject()Angular dependencies inject करता है

🧠 अभ्यास प्रश्न (Exercise)

  1. AngularJS controller को Jasmine में test करने के लिए किन steps की ज़रूरत होती है?
  2. किसी service function को कैसे isolate करके test करेंगे?
  3. inject() और beforeEach() में क्या फर्क है?

✅ आपने क्या सीखा?

  • Jasmine framework से AngularJS controller और service test करना
  • $scope, dependencies, और outputs को test करना
  • Clean test setup (beforeEach, inject)
  • Service को independently test करना
  • App में bug पकड़ने से पहले ही problem solve करना

🎯 अब आप अपने AngularJS code को confident होकर test कर सकते हैं — जिससे कोई भी code change future में break न करे!