-1

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.

  • 3
    Some may consider it very rude to be asked to respond immediately on a volunteer-driven platform. If you're in a hurry, it's possible that you should reconsider the sequence of events that got you there. – Lars Viklund Nov 11 '14 at 11:59
  • possible duplicate of [Visual Studio Team Test: How to unit test "?" operator with only Asserts() and not using any tool](http://stackoverflow.com/questions/26863782/visual-studio-team-test-how-to-unit-test-operator-with-only-asserts-and-n) – gnat Nov 11 '14 at 13:12

2 Answers2

1

Your test does definitely not test what you want to test:

[TestMethod]
[ExpectedException(typeof(NullReferenceException))] // #1
public void CreateAssociationFromXrmShouldCheckConditionalBranch()
{
    Xrm.pv_association Input = new Xrm.pv_association();
    Input = null; // #2
    PVWebApiRole.ApiModelFactory.CreateAssociationFromXrm(Input);
    var expected = default(PVWebApi.Models.Association);
    Assert.IsTrue(expected == PVWebApiRole.ApiModelFactory
                                  .CreateAssociationFromXrm(Input), "Failed");
}

@1) Why do you tell the test runner you expect a NRE if thats not what you want?

@2) How should it "create anything" if you set the value to null?

Maybe you want something like:

[TestMethod]
public void CreateAssociationFromXrmShouldCheckConditionalBranch()
{
    Xrm.pv_association Input = new Xrm.pv_association();
    var output = PVWebApiRole.ApiModelFactory.CreateAssociationFromXrm(Input);
    Assert.AreEqual(Guid.Empty, output.CreatedByUserProfileId);
}
Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
0

You are passing the entire Xrm.pv_association object as null, not just the userId. The method you are testing will throw an exception on the very first line:

AssociationId = xrmAssociation.pv_associationId.GetValueOrDefault(),  //xrmAssociation is null!

Perhaps you meant to do this in your test method instead:

Input.pv_CreatedByUserProfileID = null;
Rotem
  • 21,452
  • 6
  • 62
  • 109