Visual Studio团队测试:如何进行单元测试和QUOT;"操作者仅断言()和不使用任何工具 [英] Visual Studio Team Test: How to unit test "?" operator with only Asserts() and not using any tool

查看:167
本文介绍了Visual Studio团队测试:如何进行单元测试和QUOT;"操作者仅断言()和不使用任何工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写一些单元测试用例来测试我在C#中的Visual Studio团队测试框架代码。
下面是我想测试方法:

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 cover the conditional statements, for 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");
        }
}
}

这个测试通过,但它实际上没有什么测试我打算考即当空被传递应该将Guid.Empty。它只是抛出NullReference的例外,因此测试通过。

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.

推荐答案

我建议你写一个测试对每个单独的情况。通过这种方式,可以更方便地调整测试代码应需求的变化。

I'd suggest writing one test for each separate case. That way, you can more easily tweak the testing code should requirements change.

我会继续这样的(我也将采取猜测,假设你造型Dynamics CRM中,通过数据判断)

I'd proceed like this (and I'll also take a guess and assume you're modelling Dynamics CRM, judging by the data)

[TestMethod]
public void AssociationCreationFromXrmShouldDefaultWhenAssociationHoldingIsNull()
{
    Xrm.pv_association input = new Xrmpv_association();
    input.pv_AssociationHolding = null;

    var output = PVWebApiRole.ApiModelFactory.CreateAssociationFromXrm(Input);

    // The fact that 'output' is valid should be tested separately
    Assert.AreEqual(output.AssociationHoldingId, Guid.Empty);
}
[TestMethod]
public void AssociationCreationFromXrmShouldKeepNotNullAssociationHolding()
{
    var sampleReference = new EntityReference("yourlogicalName", Guid.Parse("00000000-0000-0000-0000-000000000000"));
    Xrm.pv_association input = new Xrmpv_association();
    input.pv_AssociationHolding = sampleReference;

    var output = PVWebApiRole.ApiModelFactory.CreateAssociationFromXrm(Input);

    // The fact that 'output' is valid should be tested separately
    Assert.AreEqual(output.AssociationHoldingId, sampleReference.Id);
}            



等等等等,两个测试每个字段,一个测试有条件的真正侧,另一个用于侧(一对夫妇的通用方法,一个用于OptionSet领域和一为的EntityReference领域可以建立并多次调用,使得代码短而快写)。

and so on and so forth, two tests for each field, one to test the true side of the conditional and one for the false side (a couple of generic methods, one for OptionSet fields and one for EntityReference fields could be built and called several times, making the code short and fast to write).

另外,我觉得你应该调整 CreateAssociationFromXrm ,使其抛出的ArgumentException 如果输入(也当然是专门一对夫妇的测试被预先写入)。

Also, I think you should tweak CreateAssociationFromXrm to make it throw an ArgumentException if input is null (a couple tests of specifically that are of course to be written beforehand).

这篇关于Visual Studio团队测试:如何进行单元测试和QUOT;"操作者仅断言()和不使用任何工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆