vendredi 30 janvier 2015

Jasmine test for an array keeps failing

I'm trying to write a jasmine test to check if data is entered into an array. I have the view model, the unit (jasmine) test file, and the stage file to set up the information for the unit tests. The error that I am getting is saying that my array is undefined. The error message says "Expected undefined to be {SiteId: 0, FirstName: 'Jon', LastName: 'Walker', ObjectState: 1}"


This is my viewmodel. I am trying to test self.addPatient:



var EF = (function () {
return {
ObjectState: {
Unchanged: 0,
Added: 1,
Modified: 2,
Deleted: 3
}
};
})();

var patientMapping = {
'Patients': {
key: function (patient) {
return ko.utils.unwrapObservable(patient.PatientId);
},
create: function (options) {
return new PatientViewModel(options.data);
}
}
};

PatientViewModel = function (data) {
var self = this;
ko.mapping.fromJS(data, {}, self);

self.flagPatientAsEdited = function () {
if (self.ObjectState() != ObjectState.Added) {
self.ObjectState(ObjectState.Modified);
}
return true;
},
self.FullName = ko.computed(function () {
return (self.FirstName() + " " + self.LastName());
});
}

SiteViewModel = function (data) {
var self = this;
ko.mapping.fromJS(data, patientMapping, self);

self.save = function () {
$.ajax({
url: "/Site/Save/",
type: "POST",
data: ko.toJSON(self),
contentType: "application/json",
success: function (data) {
if (data.siteViewModel != null) {
alert("Changes were saved successfully.");
ko.mapping.fromJS(data.siteViewModel, {}, self);
}
if (data.newLocation != null) {
window.location = data.newLocation;
}
}
});
},

self.flagSiteAsEdited = function () {
if (self.ObjectState() != ObjectState.Added) {
self.ObjectState(ObjectState.Modified);
}
return true;
},

self.addPatient = function () {
var patient = new PatientViewModel({ SiteId: 0, FirstName: "", LastName: "", ObjectState: ObjectState.Added });
self.Patients.push(patient);
},

self.deletePatient = function (patient) {
self.Patients.remove(this);

if (patient.PatientId() > 0 && self.PatientsToDelete.indexOf(patient.PatientId()) == -1) {
self.PatientsToDelete.push(patient.PatientId());
}
};
}


This is my stage file to initialize information for the jasmine tests:



//staged data for view model testing

var OBP = OBP || {};

OBP.Testing = (function () {

var reset = function () {

var isReadOnly = true;

window.isReadOnly = true;
window.ObjectState = 2;
window.FirstName = "Joe";
window.LastName = "Mitchell";
window.SiteId = 5;
window.patient = { "SiteId": 0, "FirstName": "Jon", "LastName": "Walker", "ObjectState": 1 };
window.patientDelete = { "PatientId": 1, "SiteId": 0, "FirstName": "Frank", "LastName": "Smith", "ObjectState": 1 };
};

return {
reset: reset
};
})();

OBP.Testing.reset();


This is my jasmine test itself that is failing:



/// <reference path="../../lib/jasmine.js"/>
/// <reference path="../../lib/mock-ajax.js" />
/// <reference path="../../jquery-2.1.3.min.js" />
/// <reference path="../../lib/jasmin-jquery.js" />
/// <reference path="../../bootstrap.js" />
/// <reference path="../../knockout-3.2.0.js" />
/// <reference path="../../knockout.mapping-latest.js" />
/// <reference path="SiteViewModelStage.js" />
/// <reference path="../../siteviewmodel.js" />

describe("Add patient check:", function () {
beforeEach(function () {
OBP.Testing.reset()
});

var Patients = new Array();
var viewModel = new SiteViewModel({ patient: window.patient });

it("Add patient works", function () {
expect(Patients[0]).toBe(patient);
});
});


Any help debugging my jasmine test would be greatly appreciated.


Aucun commentaire:

Enregistrer un commentaire