AngularJS Unit Testing with Karma & Jasmine

โšก Akฤฑllฤฑ ร–zet

AngularJS Unit Testing with Karma and Jasmine verifies controllers, directives, and services in isolation. Karma runs specifications inside real browsers, Jasmine supplies the assertion syntax, and angular-mocks injects dependencies without a live server.

  • ๐Ÿงฑ Tooling Split: Karma is the test runner, Jasmine is the specification framework, and angular-mocks supplies dependency injection helpers.
  • โš™๏ธ Kurulum Yolu: Install karma, karma-chrome-launcher, karma-jasmine, and karma-cli through npm, then generate karma.conf.js.
  • ๐Ÿ“ Configuration Rule: The files array must list angular.js, angular-mocks.js, application sources, and the test folder in that order.
  • ๐ŸŽ›๏ธ Controller Testing: Inject the $controller service, pass a mock $scope object, then assert the values the controller assigns.
  • ๐Ÿงฉ Directive Testing: Use $compile with $rootScope, call $digest, and inspect the rendered markup returned by the directive.
  • ๐ŸŒ End to End Layer: baลŸฤฑnatractor drives a running application and counts repeated rows produced by ng-repeat.
  • ๐Ÿ“Œ Destek Durumu: AngularJS reached end of life on 31 December 2021, so treat this workflow as legacy maintenance knowledge.

AngularJS Unit Testing with Karma and Jasmine

One of the most brilliant features of AngularJS is the Test yapmak Bu yรถn. GeliลŸtiriciler Google AngularJS'i geliลŸtirirken, test etmeyi gรถz รถnรผnde bulundurdular ve tรผm AngularJS รงerรงevesinin test edilebilir olmasฤฑnฤฑ saฤŸladฤฑlar.

In AngularJS, testing is normally carried out using Karma (framework). AngularJS testing can be carried out without Karma, but the Karma framework has such a brilliant functionality for testing AngularJS code, that it makes sense to use this framework.

  • AngularJS'de ลŸunlarฤฑ gerรงekleลŸtirebiliriz: Birim Testi denetleyiciler ve yรถnergeler iรงin ayrฤฑ ayrฤฑ.
  • Biz de performans gรถsterebiliriz end to end testing of AngularJS, which is testing from a user perspective.

โš ๏ธ Sรผrรผm notu: AngularJS (version 1.x) reached official end of life on 31 December 2021, and Protractor was retired in August 2023. The workflow on this page remains valid for maintaining existing AngularJS 1.x applications. New projects should use modern Angular with a currently supported runner.

Karma รงerรงevesinin tanฤฑtฤฑmฤฑ ve kurulumu

Karma bir otomasyon aracฤฑnฤฑ test etme created by the AngularJS team at Google. Karma'yฤฑ kullanmanฤฑn ilk adฤฑmฤฑ Karma'yฤฑ yรผklemektir. Karma, npm (modรผllerin yerel bir makineye kolay kurulumu iรงin kullanฤฑlan bir paket yรถneticisi) aracฤฑlฤฑฤŸฤฑyla kurulur.

Karma'nฤฑn Kurulumu

Karma'nฤฑn npm aracฤฑlฤฑฤŸฤฑyla kurulumu iki adฤฑmlฤฑ bir iลŸlemle gerรงekleลŸtirilir.

) 1 Adฤฑm AลŸaฤŸฤฑdaki satฤฑrฤฑ komut satฤฑrฤฑndan yรผrรผtรผn

npm install karma karma-chrome-launcher karma-jasmine --save-dev

burada,

  1. npm is the command line utility for the node package manager used for installing custom modules on any machine. It ships with node.js.
  2. Kurulum parametresi, npm komut satฤฑrฤฑ yardฤฑmcฤฑ programฤฑna kurulumun gerekli olduฤŸunu bildirir.
  3. Karma ile รงalฤฑลŸmak iรงin gereken komut satฤฑrฤฑnda belirtilen 3 kitaplฤฑk vardฤฑr.
    • karma, test amacฤฑyla kullanฤฑlacak รงekirdek kรผtรผphanedir.
    • karma-chrome-launcher, karma komutlarฤฑnฤฑn Chrome tarayฤฑcฤฑ tarafฤฑndan tanฤฑnmasฤฑnฤฑ saฤŸlayan ayrฤฑ bir kitaplฤฑktฤฑr.
    • karma-yasemin โ€“ Bu, Karma iรงin baฤŸฤฑmlฤฑ bir รงerรงeve olan yasemin yรผkler.

) 2 Adฤฑm Bir sonraki adฤฑm karma komut satฤฑrฤฑ yardฤฑmcฤฑ programฤฑnฤฑ yรผklemektir. Bu, karma satฤฑrฤฑ komutlarฤฑnฤฑ yรผrรผtmek iรงin gereklidir. Karma hattฤฑ yardฤฑmcฤฑ programฤฑ, karma ortamฤฑnฤฑ test amacฤฑyla baลŸlatmak iรงin kullanฤฑlacaktฤฑr.

Komut satฤฑrฤฑ yardฤฑmcฤฑ programฤฑnฤฑ yรผklemek iรงin komut satฤฑrฤฑndan aลŸaฤŸฤฑdaki satฤฑrฤฑ รงalฤฑลŸtฤฑrฤฑn

npm install -g karma-cli
  • karma-cli is used to install the command line interface for karma which will be used to write the karma commands in the command line interface. The -g flag installs it globally so that the karma command is available from any folder.

Karma รงerรงevesinin yapฤฑlandฤฑrฤฑlmasฤฑ

Bir sonraki adฤฑm, komut aracฤฑlฤฑฤŸฤฑyla yapฤฑlabilecek karmayฤฑ yapฤฑlandฤฑrmaktฤฑr.

karma init

Yukarฤฑdaki adฤฑm yรผrรผtรผldรผkten sonra karma, bir karma.conf.js dosyasฤฑ oluลŸturacaktฤฑr. Dosya muhtemelen aลŸaฤŸฤฑda gรถsterilen kod parรงasฤฑna benzeyecektir

files: [
  'node_modules/angular/angular.js',
  'node_modules/angular-mocks/angular-mocks.js',
  'lib/app.js',
  'tests/*.js'
]

Yukarฤฑdaki yapฤฑlandฤฑrma dosyalarฤฑ karma รงalฤฑลŸma zamanฤฑ motoruna ลŸu ลŸeyleri sรถyler:

  1. angular.js โ€“ This tells karma that your application depends on the core modules in AngularJS.
  2. angular-mocks.js โ€“ This tells karma to use the Unit Testing functionality for AngularJS from the angular-mocks.js file.
  3. Ana uygulama veya iลŸ mantฤฑฤŸฤฑ dosyalarฤฑnฤฑn tรผmรผ uygulamanฤฑzฤฑn lib klasรถrรผnde mevcuttur.
  4. Testler klasรถrรผ tรผm birim testlerini iรงerecektir.

๐Ÿ’ก ฤฐpucu: Order matters inside the files array. angular.js must load before angular-mocks.js, and both must load before your application code, otherwise the injector is not available when the specifications run.

Karmanฤฑn รงalฤฑลŸฤฑp รงalฤฑลŸmadฤฑฤŸฤฑnฤฑ kontrol etmek iรงin Sample.js adฤฑnda bir dosya oluลŸturun, aลŸaฤŸฤฑdaki kodu koyun ve test dizinine yerleลŸtirin.

describe('Sample test', function() {
  it('Condition is true', function() {
    expect('AngularJS').toBe('AngularJS');
  });
});

Yukarฤฑdaki kodun ลŸu yรถnleri vardฤฑr

  1. Aรงฤฑklama fonksiyonu testin aรงฤฑklamasฤฑnฤฑ vermek iรงin kullanฤฑlฤฑr. Bizim durumumuzda testimize 'ร–rnek test' aรงฤฑklamasฤฑnฤฑ veriyoruz.
  2. Teste isim vermek iรงin 'it' fonksiyonu kullanฤฑlฤฑr. Bizim durumumuzda testimizin adฤฑnฤฑ 'KoลŸul doฤŸrudur' olarak veriyoruz. Testin isminin anlamlฤฑ olmasฤฑ gerekmektedir.
  3. 'Beklemek' ve 'olmak' anahtar kelimelerinin birleลŸimi, test sonucunun beklenen ve gerรงek deฤŸerinin ne olduฤŸunu belirtir. Gerรงek ve beklenen deฤŸer aynฤฑ ise test baลŸarฤฑlฤฑ olacak, aksi halde baลŸarฤฑsฤฑz olacaktฤฑr.

Komut isteminde aลŸaฤŸฤฑdaki satฤฑrฤฑ รงalฤฑลŸtฤฑrdฤฑฤŸฤฑnฤฑzda yukarฤฑdaki test dosyasฤฑ รงalฤฑลŸtฤฑrฤฑlacaktฤฑr

karma start

AลŸaฤŸฤฑdaki รงฤฑktฤฑ IDE'den alฤฑnmฤฑลŸtฤฑr Webstorm yukarฤฑdaki adฤฑmlarฤฑn gerรงekleลŸtirildiฤŸi yer.

Karma ร‡erรงevesinin Yapฤฑlandฤฑrmasฤฑ

  1. ร‡ฤฑktฤฑ Karma Explorer'da gelir Webstorm. Bu pencere karma รงerรงevesinde tanฤฑmlanan tรผm testlerin yรผrรผtรผlmesini gรถsterir.
  2. Burada yรผrรผtรผlen testin aรงฤฑklamasฤฑnฤฑn โ€œร–rnek testโ€ olarak gรถsterildiฤŸini gรถrebilirsiniz.
  3. Daha sonra โ€œKoลŸul doฤŸrudurโ€ adฤฑndaki testin kendisinin yรผrรผtรผldรผฤŸรผnรผ gรถrebilirsiniz.
  4. Tรผm testlerin yanฤฑnda, tรผm testlerin geรงtiฤŸini simgeleyen yeลŸil "Tamam" simgesinin bulunduฤŸunu unutmayฤฑn.

What is Jasmine in AngularJS Unit Testing?

Karma decides where tests run, while Jasmine decides how they are written. Jasmine is a behaviour driven specification framework that supplies the vocabulary used in every example on this page, which is why the karma-jasmine adapter is installed alongside the runner.

Jasmine contributes four building blocks:

  • describe(): Groups related specifications into a suite and accepts a readable description of the unit under test.
  • it(): Declares a single specification. The name should state the behaviour being verified rather than the method being called.
  • expect() with matchers: Compares an actual value against an expected one. Common matchers include toBe, toEqual, toContain, and toBeDefined.
  • beforeEach() and afterEach(): Run setup and teardown before and after every specification, which keeps each test independent.

Jasmine also provides spies. A spy replaces a real function with a recorded stand in, so a controller can be tested without calling a live HTTP endpoint. Because Jasmine needs no DOM and no assertion library of its own, the combination of Karma and Jasmine remains the lightest way to run AngularJS JavaScript unit tests.

AngularJS Denetleyicilerini Test Etme

Karma test รงerรงevesi aynฤฑ zamanda Denetleyicileri uรงtan uca test etme iลŸlevine de sahiptir. Buna Denetleyiciler iรงinde kullanฤฑlan $scope nesnesinin test edilmesi de dahildir.

Bunu nasฤฑl baลŸarabileceฤŸimize dair bir รถrneฤŸe bakalฤฑm.

ร–rneฤŸimizde,

ร–ncelikle bir kontrolรถr tanฤฑmlamamฤฑz gerekiyor. Bu kontrolรถr aลŸaฤŸฤฑda belirtilen adฤฑmlarฤฑ gerรงekleลŸtirecektir

  1. Bir kimlik deฤŸiลŸkeni oluลŸturun ve ona 5 deฤŸerini atayฤฑn.
  2. Kimlik deฤŸiลŸkenini $scope nesnesine atayฤฑn.

Testimiz bu denetleyicinin varlฤฑฤŸฤฑnฤฑ test edecek ve ayrฤฑca $scope nesnesinin kimlik deฤŸiลŸkeninin 5'e ayarlanฤฑp ayarlanmadฤฑฤŸฤฑnฤฑ test edecektir.

ร–ncelikle aลŸaฤŸฤฑdaki รถn koลŸulun mevcut olduฤŸundan emin olmamฤฑz gerekiyor

Install the angular-mocks library via npm. This can be done by executing the below line in the command prompt

npm install angular-mocks --save-dev

Sonraki adฤฑm, teste doฤŸru dosyalarฤฑn dahil edildiฤŸinden emin olmak iรงin karma.conf.js dosyasฤฑnฤฑ deฤŸiลŸtirmektir. AลŸaฤŸฤฑdaki bรถlรผm sadece karma.conf.js dosyasฤฑnฤฑn deฤŸiลŸtirilmesi gereken dosya kฤฑsmฤฑnฤฑ gรถstermektedir

files: ['lib/angular.js', 'lib/angular-mocks.js', 'lib/index.js', 'test/*.js']
  • 'files' parametresi temel olarak Karma'ya testlerin yรผrรผtรผlmesi iรงin gerekli olan tรผm dosyalarฤฑ bildirir.
  • The angular.js and angular-mocks.js file are required to run AngularJS unit tests
  • ฤฐndex.js dosyasฤฑ denetleyiciye ait kodumuzu iรงerecektir
  • Test klasรถrรผ tรผm AngularJS testlerimizi iรงerecektir

Below is our AngularJS code which will be stored as a file Index.js in the test folder of our application.

AลŸaฤŸฤฑdaki kod sadece ลŸu ลŸeyleri yapar

  1. Bir oluลŸturma AngularJS modรผlรผ sampleApp denir
  2. AngularJSController adฤฑnda bir denetleyici oluลŸturun
  3. ID adฤฑnda bir deฤŸiลŸken oluลŸturun, ona 5 deฤŸerini verin ve onu $scope nesnesine atayฤฑn
var sampleApp = angular.module('sampleApp', []);
sampleApp.controller('AngularJSController', function($scope) {
  $scope.ID = 5;
});

Yukarฤฑdaki kod baลŸarฤฑyla yรผrรผtรผldรผฤŸรผnde, bir sonraki adฤฑm bir oluลŸturmak olacaktฤฑr. Test Durumu Kodun doฤŸru ลŸekilde yazฤฑldฤฑฤŸฤฑndan ve yรผrรผtรผldรผฤŸรผnden emin olmak iรงin.

Testimizin kodu aลŸaฤŸฤฑdaki gibi olacaktฤฑr.

Kod, test klasรถrรผne yerleลŸtirilecek ControllerTest.js adlฤฑ ayrฤฑ bir dosyada olacak. AลŸaฤŸฤฑdaki kod yalnฤฑzca ลŸu temel ลŸeyleri yapar

  1. beforeEach function โ€“ This function is used to load our AngularJS module called โ€˜sampleAppโ€™ before the test run. Note that this is the name of the module in an index.js file.
  2. The $controller object is created as a mockup object for the controller โ€œAngularJSControllerโ€ which is defined in our index.js file. In any sort of Unit Testing, a mock object represents a dummy object which will actually be used for the testing. This mock object will actually simulate the behavior of our controller.
  3. beforeEach(inject(function(_$controller_)) โ€“ Bu, gerรงek denetleyici gibi davranmasฤฑ iรงin testimize sahte nesneyi enjekte etmek iรงin kullanฤฑlฤฑr.
  4. var $kapsam = {}; Bu, $scope nesnesi iรงin oluลŸturulan sahte bir nesnedir.
  5. var controller = $controller(โ€˜AngularJSControllerโ€™, { $scope: $scope }); โ€“ Here we are checking for the existence of a controller named โ€˜AngularJSControllerโ€™. In here we are also assigning all variables from our $scope object in our controller in the Index.js file to the $scope object in our test file
  6. Son olarak $scope.ID deฤŸerini 5 ile karลŸฤฑlaลŸtฤฑrฤฑyoruz.
describe('AngularJSController', function() {
  beforeEach(module('sampleApp'));

  var $controller;

  beforeEach(inject(function(_$controller_){
    $controller = _$controller_;
  }));

  describe('$scope.ID', function() {
    it('Check the scope object', function() {
      var $scope = {};
      var controller = $controller('AngularJSController', { $scope: $scope });
      expect($scope.ID).toEqual(5);
    });
  });
});

Yukarฤฑdaki test karma tarayฤฑcฤฑsฤฑnda รงalฤฑลŸacak ve รถnceki konuda gรถsterilenle aynฤฑ baลŸarฤฑlฤฑ sonucu verecektir.

AngularJS Direktiflerini Test Etme

Karma test รงerรงevesi, รถzel yรถnergeleri test etme iลŸlevine de sahiptir. Bu, ลŸablonu da iรงerir.URLร–zel yรถnergeler iรงerisinde kullanฤฑlan 'ler.

Bunu nasฤฑl baลŸarabileceฤŸimize dair bir รถrneฤŸe bakalฤฑm.

ร–rneฤŸimizde, รถncelikle aลŸaฤŸฤฑdaki iลŸlemleri yapan รถzel bir yรถnerge tanฤฑmlayacaฤŸฤฑz

  1. sampleApp adฤฑnda bir AngularJS modรผlรผ oluลŸturun
  2. Create a custom directive with the name โ€“ guru99
  3. "Bu AngularJS Testidir" metnini gรถrรผntรผleyen baลŸlฤฑk etiketine sahip bir ลŸablon dรถndรผren bir iลŸlev oluลŸturun.
var sampleApp = angular.module('sampleApp', []);
sampleApp.directive('guru99', function () {
  return {
    restrict: 'E',
    replace: true,
    template: '<h1>This is AngularJS Testing</h1>'
  };
});

Yukarฤฑdaki kod baลŸarฤฑyla รงalฤฑลŸtฤฑrฤฑldฤฑktan sonraki adฤฑm, kodun dรผzgรผn bir ลŸekilde yazฤฑldฤฑฤŸฤฑndan ve yรผrรผtรผldรผฤŸรผnden emin olmak iรงin bir test senaryosu oluลŸturmak olacaktฤฑr. Testimizin kodu aลŸaฤŸฤฑda gรถsterildiฤŸi gibi olacaktฤฑr.

Kod, test klasรถrรผne yerleลŸtirilecek olan DirectionTest.js adlฤฑ ayrฤฑ bir dosyada olacaktฤฑr. AลŸaฤŸฤฑdaki kod yalnฤฑzca ลŸu temel ลŸeyleri yapar

  1. beforeEach function โ€“ This function is used to load our AngularJS module called โ€˜sampleAppโ€™ before the test run.
  2. The $compile service is used to compile the directive. This service is mandatory and needs to be declared so that AngularJS can use it to compile our custom directive.
  3. The $rootScope is the primary scope of any AngularJS application. We have seen the $scope object of the controller in earlier chapters. Well, the $scope object is the child object of the $rootScope object. The reason this is declared here is because we are making a change to an actual HTML tag in the DOM via our custom directive. Hence, we need to use the $rootScope service which actually listens or knows when any change happens from within an HTML document.
  4. var element = $compile(โ€œ<guru99></guru99>โ€)($rootScope) โ€“ This is used to check whether our directive gets injected as it should. The directive is registered with the camel case name guru99, so AngularJS matches the lowercase element <guru99> in the markup. Hence this statement is used to make that check.
  5. expect(element.html()).toContain(โ€œThis is AngularJS Testingโ€) โ€“ This is used to instruct the expect function that it should find the element(in our case the h1 tag) to contain the innerHTML text of โ€œThis is AngularJS Testingโ€.
describe('Unit testing directives', function() {
  var $compile,
      $rootScope;
  beforeEach(module('sampleApp'));

  beforeEach(inject(function(_$compile_, _$rootScope_){
    $compile = _$compile_;
    $rootScope = _$rootScope_;
  }));

  it('Check the directive', function() {
    // Compile a piece of HTML containing the directive
    var element = $compile("<guru99></guru99>")($rootScope);
    $rootScope.$digest();
    expect(element.html()).toContain("This is AngularJS Testing");
  });
});

Yukarฤฑdaki test karma tarayฤฑcฤฑsฤฑnda รงalฤฑลŸacak ve รถnceki konuda gรถsterilenle aynฤฑ baลŸarฤฑlฤฑ sonucu verecektir.

End to End Testing AngularJS applications

Karma test รงerรงevesi, Pro adฤฑ verilen bir รงerรงeveyle birlikte kullanฤฑlฤฑr.tracTor, bir web uygulamasฤฑnฤฑ uรงtan uca test etme iลŸlevine sahiptir.

Yani bu sadece direktiflerin ve denetleyicilerin test edilmesi deฤŸil aynฤฑ zamanda bir HTML sayfasฤฑnda gรถrรผnebilecek diฤŸer her ลŸeyin test edilmesidir.

Bunu nasฤฑl baลŸarabileceฤŸimize dair bir รถrneฤŸe bakalฤฑm.

AลŸaฤŸฤฑdaki รถrneฤŸimizde ng-repeat direktifini kullanarak veri tablosu oluลŸturan bir AngularJS uygulamamฤฑz olacak.

  1. ร–ncelikle โ€œรถฤŸreticiโ€ adฤฑ verilen bir deฤŸiลŸken oluลŸturuyoruz ve ona tek adฤฑmda bazฤฑ anahtar/deฤŸer รงiftlerini atadฤฑk. Tablo gรถrรผntรผlenirken her anahtar/deฤŸer รงifti veri olarak kullanฤฑlacaktฤฑr. ร–ฤŸretici deฤŸiลŸken daha sonra kapsam nesnesine atanฤฑr, bรถylece ona gรถrรผnรผmรผmรผzden eriลŸilebilir.
  2. Tablodaki her veri satฤฑrฤฑ iรงin ng-repeat direktifini kullanฤฑyoruz. Bu yรถnerge, ptutor deฤŸiลŸkenini kullanarak eฤŸitim kapsamฤฑ nesnesindeki her anahtar/deฤŸer รงiftinden geรงer.
  3. Son olarak ลŸunu kullanฤฑyoruz: anahtar deฤŸer รงiftleriyle birlikte etiketleyin (ptutor.Name ve ptutor.Description) tablo verilerini gรถrรผntรผlemek iรงin.
<table>
  <tr ng-repeat="ptutor in tutorial">
    <td>{{ ptutor.Name }}</td>
    <td>{{ ptutor.Description }}</td>
  </tr>
</table>
</div>
<script type="text/javascript">
var app = angular.module('DemoApp', []);
app.controller('DemoController', function($scope) {
  $scope.tutorial = [
    {Name: "Controllers", Description: "Controllers in action"},
    {Name: "Models", Description: "Models and binding data"},
    {Name: "Directives", Description: "Flexibility of Directives"}
  ];
});

Yukarฤฑdaki kod baลŸarฤฑyla รงalฤฑลŸtฤฑrฤฑldฤฑktan sonraki adฤฑm, kodun dรผzgรผn bir ลŸekilde yazฤฑldฤฑฤŸฤฑndan ve yรผrรผtรผldรผฤŸรผnden emin olmak iรงin bir test senaryosu oluลŸturmak olacaktฤฑr. Testimizin kodu aลŸaฤŸฤฑda gรถsterildiฤŸi gibi olacaktฤฑr.

Testimiz aslฤฑnda ng-repeat direktifini test edecek ve yukarฤฑdaki รถrnekte olmasฤฑ gerektiฤŸi gibi 3 satฤฑrlฤฑk veri iรงerdiฤŸinden emin olacak.

ร–ncelikle aลŸaฤŸฤฑdaki รถn koลŸulun mevcut olduฤŸundan emin olmamฤฑz gerekiyor

profesyoneli yรผkleyintracnpm aracฤฑlฤฑฤŸฤฑyla Tor kรผtรผphanesini ekleyebilirsiniz. Bunu, komut isteminde aลŸaฤŸฤฑdaki satฤฑrฤฑ รงalฤฑลŸtฤฑrarak yapabilirsiniz.

npm install -g protractor

Testimizin kodu aลŸaฤŸฤฑdaki gibi olacaktฤฑr.

Kod, test klasรถrรผne yerleลŸtirilecek olan CompleteTest.js adlฤฑ ayrฤฑ bir dosyada olacak. AลŸaฤŸฤฑdaki kod yalnฤฑzca ลŸu temel ลŸeyleri yapar

  1. Tarayฤฑcฤฑ iลŸlevi profesyonel tarafฤฑndan saฤŸlanmaktadฤฑr.tracTor kรผtรผphanesini kullanฤฑr ve yukarฤฑda gรถsterilen kodla birlikte AngularJS uygulamamฤฑzฤฑn sitemizde รงalฤฑลŸtฤฑฤŸฤฑnฤฑ varsayar. URL โ€“ http://localhost:8080/Guru99 /
  2. var list = element.all(by.repeater(โ€˜ptutor in tutorialโ€™)); -This line of code is actually fetching the ng-repeat directive which is populated by the code โ€˜ptutor in tutorialโ€™. The element and by.repeater are special keywords provided by the protracng-repeat yรถnergesinin ayrฤฑntฤฑlarฤฑnฤฑ almamฤฑzฤฑ saฤŸlayan bir Tor kรผtรผphanesi.
  3. beklenti(list.count()).toEqual(3); โ€“ Son olarak, ng-repeat direktifinin bir sonucu olarak tablomuzda gerรงekten 3 รถฤŸenin doldurulduฤŸunu gรถrmek iรงin wait fonksiyonunu kullanฤฑyoruz.
describe('Unit testing end to end', function() {
  beforeEach(function() {
    browser.get('http://localhost:8080/Guru99/');
  });

  it('Check the ng directive', function() {
    var list = element.all(by.repeater('ptutor in tutorial'));
    expect(list.count()).toEqual(3);
  });
});

Yukarฤฑdaki test karma tarayฤฑcฤฑsฤฑnda รงalฤฑลŸacak ve รถnceki konuda gรถsterilenle aynฤฑ baลŸarฤฑlฤฑ sonucu verecektir.

Karma vs Jasmine vs Protractor: Key Differences

The three tools used across this page are often confused because they appear in the same command line. Each one occupies a different layer of the testing stack, as the table below makes clear.

Parametre Karma Yasemin baลŸฤฑnatractor
Kategoriler Test runner Specification framework End to end automation framework
Asฤฑl iลŸ Launches browsers and reports results Provides describe, it, and expect Drives a live application in a real browser
Test seviyesi birim ve bรผtรผnleลŸme Unit and integration BaลŸtan sona
Needs a running server Yok hayฤฑr Yok hayฤฑr Evet
yapฤฑlandฤฑrma dosyasฤฑ karma.conf.js None, it runs inside a runner iรงintractor.conf.js
Mevcut durum Deprecated, maintenance only Aktif bakฤฑmlฤฑ Retired in August 2023

In short, Jasmine writes the test, Karma executes it, and Protractor extends the same Jasmine syntax to a fully rendered page.

Best Practices for AngularJS Unit Testing

A small set of habits keeps an AngularJS suite fast and trustworthy as the application grows.

  1. Test one unit at a time. Load only the module under test inside beforeEach, and mock every collaborator so a failure points at a single file.
  2. Never call a real backend. Use the $httpBackend service from angular-mocks to return canned responses. Tests then run offline and produce identical results on every machine.
  3. Keep specification names behavioural. A name such as โ€œreturns 5 when the controller initialisesโ€ reads better in a report than โ€œtest1โ€.
  4. Always flush the digest cycle. Call $rootScope.$digest() after changing scope data, otherwise bindings and watchers never update and assertions fail for the wrong reason.
  5. Measure coverage, but judge it carefully. Add karma-coverage to see untested branches. High coverage with weak assertions still hides defects.
  6. Run the suite in continuous integration. Use a headless Chrome launcher so the same specifications execute on every commit rather than only on a developer machine.

Applying these rules turns the examples above from isolated demonstrations into a regression safety net for a legacy AngularJS codebase. For the wider picture, see the AngularJS tutorial serisi.

SSS

The module holding that provider was not loaded. Add it inside beforeEach(module(โ€˜yourModuleโ€™)) and confirm angular-mocks.js is listed in karma.conf.js after angular.js, otherwise the injector cannot resolve the dependency.

Install karma-chrome-launcher and set the browsers option to ChromeHeadless in karma.conf.js. The suite then runs inside continuous integration pipelines and containers where no display server is available.

Yes. AI coding assistants read a controller and draft Jasmine specifications with mock scopes and matchers. Review the output carefully, because generated assertions often restate the implementation instead of verifying the intended behaviour.

Yes. AI tools translate Jasmine specifications into Jest or Vitest syntax and flag angular-mocks calls that have no direct equivalent. Manual verification remains essential, since dependency injection patterns rarely convert cleanly.

Most teams target seventy to eighty percent statement coverage on controllers and services. Chasing a higher figure usually means testing templates and third party libraries, which adds maintenance cost without catching real defects.

Bu yazฤฑyฤฑ ลŸu ลŸekilde รถzetleyin: