How to do it...

  1. Create a new empty ASP.NET Web Application in Visual Studio, as shown here:
  1. Download and add Qunit.js from https://qunitjs.com/.
  2. Under the scripts folder, add the common.js library created in the first recipe of this chapter:
var packtNs = packtNs || {}; 
packtNs.common = packtNs.common || {};

packtNs.common.populateWithTodaysDate = function()
{
if (Xrm.Page.getAttribute("packt_supervisor").getValue()
!== null && Xrm.Page.getAttribute("packt_postgraduatestartdate").
getValue() === null)
{
Xrm.Page.getAttribute("packt_postgraduatestartdate").
setValue(new Date());
}
}
  1. Add the following code in the Xrm.Fake.js file:
var Xrm = {}; 
Xrm.Page = {}
var Attribute = function (attributeName) {
this.attributeName = attributeName,
this.setValueCounter = 0;
this.getValue = function () {
if (this.attributeName === ("packt_supervisor")) {
return "Sample Value";
}
return null;
},
this.setValue = function (value) {
this.setValueCounter += 1;
},
this.getCountFunctionCalls = function () {
return this.setValueCounter;
}
}
var pageAttributes = {}
Xrm.Page.getAttribute = function (attributeName) {
if (pageAttributes[attributeName]) {
return pageAttributes[attributeName];
}
pageAttributes[attributeName] = new
Attribute(attributeName);
return pageAttributes[attributeName];
}
  1. Add the following code in the common.test.js file:
/// <reference path="common.js"/> 
/// <reference path="qunit-2.0.1.js" />
/// <reference path="FakeXrmPage.js"/>
test("Xrm test populateWithTodaysDate", function () {
// Act
packtNs.common.populateWithTodaysDate();

//Assert equal(Xrm.Page.getAttribute("packt_postgraduatestartdate")
. getCountSetValueCalls(), 1, "PostGraduate attribute
setValue called exactly once");
});
  1. Run the test by clicking on the ReSharper icon next to your test method and select Run:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.202.209