I have developed an SPA application using knockout and breeze, I have been using qunitJS for unit testing which works well on browser for my project. Recently I have found a plugin "Chutzpah" for VS2012, which can work well for testing qunit, which can check the test cases and respond for the Test though I am using breeze my test cases includes:
Creating, Deleting, Modifying entities on client side
Save changes on server side using breeze controller to SQLServer
Here the problem is the breeze test cases fails when I am testing in Chutzpah because they are not getting client side information like(Metadata/ ControllerMethods) which is called by the browser through the url defined in Breezeconfig. My question is that how can we perform unit test on breeze on server side like real unit test cases do. Suggestions for using new plugins or libraries which can actually make me do the unit testing of breeze are welcome
Here is the snippet for my testing project:
asyncTest("can save nothing", function () {
expect(1);
newEm().saveChanges()
.then(function(saveResult) {
equal(saveResult.entities.length, 0, 'succeeded in saving nothing');
})
.catch(handleFail).finally(start);
});
asyncTest("can save a new Customer entity", function () {
expect(1);
// Create and initialize entity to save
var em = newEm();
var customer = em.createEntity('Customer', {
CustomerID: newGuidComb(),
CompanyName: 'Test1 ' + new Date().toISOString()
});
em.saveChanges()
.then(function (saveResults) {
ok(!!saveResults.entities[0], ' should have saved new Customer with CustomerID ' +
customer.getProperty('CustomerID'));
})
.catch(handleFail).finally(start);
});
asyncTest("can modify my own Customer entity", function () {
expect(2);
var timestamp = new Date().toISOString();
var em = newEm();
var customer = em.createEntity('Customer', {
CustomerID: newGuidComb(),
CompanyName: "Test2A " + timestamp
});
em.saveChanges().then(modifyCustomer).fail(handleSaveFailed).fin(start);
function modifyCustomer(saveResults) {
var saved = saveResults.entities[0];
ok(saved && saved === customer,
"save of added customer should have succeeded");
customer.CompanyName("Test2M " + timestamp);
return em.saveChanges()
.then(confirmCustomerSaved);
}
function confirmCustomerSaved(saveResults) {
var saved = saveResults.entities[0];
ok(saved && saved === customer,
"save of modified customer, '{0}', should have succeeded"
.format(saved && saved.CompanyName()));
}
});
The short answer is to set up the tests so that server communication is not required. Capture the metadata so that you can load it from a script before you run your tests. Create mock data in Breeze so that you query from the local EntityManager instead of the server.
And, of course, you should separate the business logic of your application (the kind that needs a lot of testing) from the server-communication bits.