Hello This is Karma

Steps to utilize Karma:

  • First install Karma at universal location cmd C:\Users\userID\Documents\GitHub>
  • cmd > npm install karma --save-dev
  • cmd > npm install karma-jasmine --save-dev
  • cmd > npm install -g karma-cli
  • c:\projectPath\projectname >.\node_modules\.bin\karma init
  • Once you run above mentioned command, you will need to provide following information. Refer this Link

    • Which testing framework do you want to use? (Answer should be Jasmine)
    • Do you want to use Require.js? Answ: I replied with No.
    • Do you want to capture a browser automatically? Answ: Chrome
    • What is the location of your source and test files? Answ: particular module folder added and particular test module function added.
    • Should any of the files included by the previous patterns be excluded? Answ: left blank
    • Do you want Karma to watch all the files and run the tests on change? Answ: yes
  • Then to start Karma enter below command cmd > .\node_modules\.bin\karma start

  • Karma use Jasmine as a framework

  • Below is example having two methods add and Substract. We are testing those functions with Jasmine test cases.

Writing Unit Test Case using Jasmine


Suite’s are like class in object oriented languages like Java, C# which can contain one or more test case methods. In Jasmine you can define a suite by calling global Jasmine global function called describe () which takes two arguments. The first argument is the name of the suite and the second argument is the JavaScript function containing initialization functions and specs.
HTML Describe ("This is a suite",function (){ });


Spec in Jasmine is a test case which can be defined by calling global Jasmine function called it () which again takes two arguments like suite. The first argument is the name of the spec and the second argument is the JavaScript function containing one or more expectations. If one run’s any of the spec temporarily then you can declare it as it(), these spec are called pending specs.
HTML Describe ("This is a suite",function (){ It ("This is a spec",function (){ });});


Expectation in Jasmine is like assertions. Expectation is built with Jasmine function called expect (). Expect function takes a single argument which represents the actual value and compares it to the expected value with the help of a matcher function. Jasmine’s matcher functions do Boolean comparison between the actual and the expected values. Jasmine framework is loaded with plenty of matchers but if there is a need you can also define you own custom matcher.
Describe ("This is a suite",function (){ it("This is a spec with expectations",function(){ var expectedValue = a; var actualValue = 12; expect(expectedValue).toBe(actualValue ); expect(expectedValue).not.toBe(actualValue); }); });

Setup and Teardown

You can implement initialization and cleanup logic inside the Jasmine’s following global functions.

  • The beforeEach() is called once before every spec in the describe is executed
  • The afterEach() is called once after every spec in the describe is executed
  • The beforeAll() is called only once before any of the specs in the describe is executed
  • The afterAll() is called after all the specs in the describe are executed

Following additional matchers are available specifically for interacting with spies.

  • toHaveBeenCalled() : This Matcher returns true if the method on which spy was created is called
  • toHaveBeenCalledWith() : This Matcher returns true if any of the call to the function on which Spy was created has the same argument list as declared in the respective Spy’s argument list

You can chain call to a Spy

  • and.callThrough() : This tracks all calls to a function but also want to delegate call to the actual implementation.
  • and.returnValue() : For all calls to a Spy to return a specific value
  • and.callFake() : For a Spy to be delegated to a supplied function
  • and.throwError() : For all calls to a Spy to for an error on specific values

Following are some of the methods available for tracking calls to a Spy,

  • .calls.any() : Returns true if one or more calls have been made to the Spy. Returns false if no call made to the Spy.
  • .calls.count() : Returns the count of how many times the spy was called
  • .calls.argsFor(index) : Returns the arguments passed to the call specified in the index param
  • .calls.allArgs() : Returns the list of set of arguments made in various calls to the Spy
  • .calls.reset() : Resets/clears all the tracking information for a spy

Reference Site:

Main Menu -> Angular JS Learning Index

223 0 0