Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of spec.js file in AngularJS

Tags:

angularjs

This might sound a stupid question, but I want to know its answer. What is the spec.js file in AngularJS and what is its use? Is it used for testing purpose?

EDIT- Below is the code of file phone-detail.component.spec.js

'use strict';  describe('phoneDetail', function() {    // Load the module that contains the `phoneDetail` component before each test   beforeEach(module('phoneDetail'));    // Test the controller   describe('PhoneDetailController', function() {     var $httpBackend, ctrl;     var xyzPhoneData = {       name: 'phone xyz',       images: ['image/url1.png', 'image/url2.png']     };      beforeEach(inject(function($componentController, _$httpBackend_, $routeParams) {       $httpBackend = _$httpBackend_;       $httpBackend.expectGET('phones/xyz.json').respond(xyzPhoneData);        $routeParams.phoneId = 'xyz';        ctrl = $componentController('phoneDetail');     }));      it('should fetch the phone details', function() {       jasmine.addCustomEqualityTester(angular.equals);        expect(ctrl.phone).toEqual({});        $httpBackend.flush();       expect(ctrl.phone).toEqual(xyzPhoneData);     });    });  }); 
like image 874
HardikT Avatar asked Jun 27 '16 06:06

HardikT


People also ask

What is a spec js file?

Spec is short for "Specification" as @DavinTryon suggested above. Specification in terms of a test refer to the technical details of a given feature or application which must be fulfilled. The best way to think of this is as the technical specifications for a given unit of code to pass successfully.

What are specs in Jasmine?

Specs. Specs are defined by calling the global Jasmine function it, which, like describe takes a string and a function. The string is the title of the spec and the function is the spec, or test. A spec contains one or more expectations that test the state of the code.

How do I run a test case in AngularJS?

Testing in AngularJS is achieved by using the karma framework, a framework which has been developed by Google itself. The karma framework is installed using the node package manager. The key modules which are required to be installed for basic testing are karma, karma-chrome-launcher ,karma-jasmine, and karma-cli.

In which folder are the most important files stored in AngularJS?

top-level “app” folder : all angular files should be placed within a folder that could be named as “app”. “app” is the top-level folder. app/components folder: one or more components can be placed within “app/components” folder.


1 Answers

Use of spec.js is for writing you unit test cases for your angular application. We write test cases in angular using Jasmine & Karma.

Jasmine is a Behavior Driven Development testing framework for JavaScript. It does not rely on browsers, DOM, or any JavaScript framework. Thus it's suited for websites, Node.js projects, or anywhere that JavaScript can run.

https://github.com/jasmine/jasmine

Karma is essentially a tool which spawns a web server that executes source code against test code for each of the browsers connected. The results of each test against each browser are examined and displayed via the command line to the developer such that they can see which browsers and tests passed or failed.

https://karma-runner.github.io/1.0/index.html

like image 134
Arvin Avatar answered Sep 17 '22 19:09

Arvin