且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Visual Studio团队测试:如何进行单元测试和QUOT;"操作者仅断言()和不使用任何工具

更新时间:2023-02-17 09:26:13

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

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).