jeudi 5 février 2015

Unit testing javascript using Jasmine framework

I am very new to Jasmine, infact I just started today and don't have prior knowledge writing JS unit test cases.I am writing unit test using Jasmine for an external javascript file.Unfortunately,I am not quite sure as to how to write a unit test for a function without any parameters.All the examples I have seen so far show a function with atleast one parameter and some value being returned.


The javascript file with the function to be tested.(CreateProvider.js)



var provider = function () {
var self = this;

self.formatPhoneNumber = function () {
if (self.contactNumber().length == 10) {
self.contactNumber(self.contactNumber().replace(/(\d{3})(\d{3})(\d{4})/, "($1)$2-$3"));
$("#contactNumber").rules("remove", "max");
$("#contactNumber").rules("add", { maxlength: 13 });
}
};

self.formatSSN = function () {
if (self.SSN().length == 9) {
self.SSN(self.SSN().replace(/(\d{3})(\d{2})(\d{4})/, "$1-$2-$3"));
$("#SSN").rules("remove", "max");
$("#SSN").rules("add", { maxlength: 11 });
}
};
};


Now ,I have created another project for jasmine testing.Here I have created another Javascript file.



/// <reference path = ""../../App_Scripts/CreateProvider.js"" />

describe("providerTests", function () {
var Provider;

beforeEach(function () {
Provider = new provider();
});

it("should format a phone number", function () {
//Unit test a function without parameters
});


});


Could someone please guide me in the right direction.


Aucun commentaire:

Enregistrer un commentaire