Course Progress 95%

AngularJS Testing

AngularJS application की quality और reliability बनाए रखने के लिए Testing बहुत जरूरी है। Testing से यह ensure होता है कि application का हर part सही तरह से काम कर रहा है और future changes से existing features break न हों।

इस chapter में आप सीखेंगे:

  • AngularJS में testing क्यों जरूरी है
  • Testing के types (Unit Testing, End-to-End Testing)
  • Jasmine और Karma का role
  • Controllers, Services और Filters की testing
  • Best practices for AngularJS testing

AngularJS में Testing क्यों जरूरी है

Testing से:

  • Bugs जल्दी पकड़ में आते हैं
  • Code refactor करना safe होता है
  • Application stable रहती है
  • Team collaboration बेहतर होता है

Large AngularJS applications में testing mandatory हो जाती है।

AngularJS Testing के Types

AngularJS में mainly दो types की testing होती है:

  • Unit Testing – Individual components test करना
  • End-to-End (E2E) Testing – Complete application flow test करना

Tools Used in AngularJS Testing

AngularJS ecosystem में common tools:

  • Jasmine – Testing framework
  • Karma – Test runner
  • Protractor – E2E testing tool

Unit Testing with Jasmine

Jasmine behavior-driven testing framework है।

Basic Jasmine Test Structure

describe("Sample Test", function() {
  it("should return true", function() {
    expect(true).toBe(true);
  });
});

Explanation

  • describe() test suite define करता है
  • it() individual test case होता है
  • expect() assertion होता है

Testing AngularJS Controller

describe("myCtrl Test", function() {
  beforeEach(module("myApp"));

  var $controller, $scope;

  beforeEach(inject(function(_$controller_, $rootScope) {
    $controller = _$controller_;
    $scope = $rootScope.$new();
    $controller("myCtrl", { $scope: $scope });
  }));

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

Code Explanation

  • module() app module load करता है
  • $controller controller instantiate करता है
  • $scope mock scope create करता है
  • Controller logic verify होती है

Testing AngularJS Service

describe("myService Test", function() {
  beforeEach(module("myApp"));

  var myService;

  beforeEach(inject(function(_myService_) {
    myService = _myService_;
  }));

  it("should return data", function() {
    expect(myService.getData()).toBeDefined();
  });
});

Explanation

  • Service directly inject होती है
  • Business logic isolated तरीके से test होती है

Testing Filters

describe("reverseText Filter", function() {
  beforeEach(module("myApp"));

  var filter;

  beforeEach(inject(function($filter) {
    filter = $filter("reverseText");
  }));

  it("should reverse text", function() {
    expect(filter("abc")).toBe("cba");
  });
});

Explanation

  • $filter service filter access करने के लिए use होती है
  • Filter logic independent test होती है

Mocking $http Requests

describe("HTTP Test", function() {
  var $httpBackend, service;

  beforeEach(inject(function(_$httpBackend_, myService) {
    $httpBackend = _$httpBackend_;
    service = myService;
  }));

  it("should fetch data", function() {
    $httpBackend.expectGET("data.json").respond(200, {});
    service.getData();
    $httpBackend.flush();
  });
});

Explanation

  • $httpBackend fake HTTP responses देता है
  • Real server call avoid होती है
  • Test fast और reliable रहता है

End-to-End Testing with Protractor

Protractor real browser में application test करता है।

Example E2E Test

describe("Homepage Test", function() {
  it("should display title", function() {
    browser.get("index.html");
    expect(element(by.binding("title")).getText()).toBe("Home");
  });
});

Explanation

  • Real user behavior simulate होता है
  • UI + backend integration test होती है

Karma Configuration (Basic)

module.exports = function(config) {
  config.set({
    frameworks: ["jasmine"],
    browsers: ["Chrome"]
  });
};

Explanation

  • Karma test runner configure होता है
  • Tests automatically run होते हैं

Common Testing Mistakes

  • Tests न लिखना
  • Too much mocking
  • Flaky E2E tests
  • Testing logic को skip करना

Best Practices

  • Unit tests पहले लिखें
  • Small, isolated tests बनाएँ
  • Controllers thin रखें
  • Services ज्यादा test करें
  • CI pipeline में tests run करें

Summary

इस chapter में आपने सीखा:

  • AngularJS testing का importance
  • Unit और E2E testing concepts
  • Jasmine, Karma और Protractor का use
  • Controllers, services और filters testing
  • Testing best practices