I need to write some unit test cases to test my code in C# Visual Studio Team Test framework. Below is the method I want to test:
public static Association CreateAssociationFromXrm(Xrm.pv_association xrmAssociation)
{
return new Association
{
AssociationId = xrmAssociation.pv_associationId.GetValueOrDefault(),
Name = xrmAssociation.pv_Name,
BusinessId = xrmAssociation.pv_BusinessID,
ParcelId = xrmAssociation.pv_ParcelID,
AssociationHoldingId = (xrmAssociation.pv_AssociationHolding != null) ? xrmAssociation.pv_AssociationHolding.Id : Guid.Empty,
AssociationCategoryId = (xrmAssociation.pv_Category != null) ? xrmAssociation.pv_Category.Id : Guid.Empty,
EmailAddress = xrmAssociation.pv_PrimaryEmail,
Description = xrmAssociation.pv_Description,
IsDevelopementComplete = xrmAssociation.pv_IsDevelopmentComplete,
Fax = xrmAssociation.pv_Fax,
AlternateEmail = xrmAssociation.pv_AlternateEmail,
WorkPhone1 = xrmAssociation.pv_WorkPhone1,
WorkPhone2 = xrmAssociation.pv_WorkPhone2,
Website = xrmAssociation.pv_Website,
DevelopmentCount = xrmAssociation.pv_DevelopmentOwnedCount,
HomeDescription = xrmAssociation.pv_HomeDescription,
DateTurnedOver = xrmAssociation.pv_DateTurnedOver,
OwnersOccupiedCount = xrmAssociation.pv_OwnersOccupiedCount.HasValue ? xrmAssociation.pv_OwnersOccupiedCount.Value : 0,
RentalsOccupiedCount = xrmAssociation.pv_RentalsOccupiedCount.HasValue ? xrmAssociation.pv_RentalsOccupiedCount.Value : 0,
RentalCapCount = xrmAssociation.pv_RentalCapCount.HasValue ? xrmAssociation.pv_RentalCapCount.Value : 0,
HomeMaxCount = xrmAssociation.pv_HomeMaxCount.HasValue ? xrmAssociation.pv_HomeMaxCount.Value : 0,
Address1 = xrmAssociation.pv_Address1,
Address2 = xrmAssociation.pv_Address2,
State = xrmAssociation.pv_State,
City = xrmAssociation.pv_City,
Zip = xrmAssociation.pv_ZIP,
County = xrmAssociation.pv_County,
CommunityText = xrmAssociation.pv_CommunityText,
FederalCompanyClassificationId = xrmAssociation.pv_FederalCompanyClassificationID != null ? xrmAssociation.pv_FederalCompanyClassificationID.Id : Guid.Empty,
Domain = xrmAssociation.pv_Domain,
CreatedByUserProfileId = xrmAssociation.pv_CreatedByUserProfileID != null ? xrmAssociation.pv_CreatedByUserProfileID.Id : Guid.Empty,
ModifiedByUserProfileId = xrmAssociation.pv_ModifiedByUserProfileID != null ? xrmAssociation.pv_ModifiedByUserProfileID.Id : Guid.Empty
};
}
In the above method, I need to write unit test cases to test especially the "if-else" logic statement. Example - "CreatedByUserProfileId = xrmAssociation.pv_CreatedByUserProfileID != null ? xrmAssociation.pv_CreatedByUserProfileID.Id: Guid.Empty"
So I wrote the following unit test method:
namespace PVWebApi.Test
{
[TestClass]
public class ApiModelFactoryTest
{
[TestMethod]
[ExpectedException(typeof(NullReferenceException), "A userId of null was inappropriately allowed.")]
public void CreateAssociationFromXrmShouldCheckConditionalBranch()
{
Xrm.pv_association Input = new Xrm.pv_association();
Input = null;
PVWebApiRole.ApiModelFactory.CreateAssociationFromXrm(Input);
var expected = default(PVWebApi.Models.Association);
Assert.IsTrue(expected == PVWebApiRole.ApiModelFactory.CreateAssociationFromXrm(Input), "Failed");
}
}
}
This test passed, but it is actually not testing what I intend to test i.e. It should assign "Guid.Empty" when "null" is being passed. It just throws the exceptions of NullReference and thus the test passes.