如何在C#中为其中的虚方法执行抽象类的Moq测试?

时间:2017-02-26 13:46:44

标签: c# moq

我想对下面的抽象Streaming类进行测试,该类有两个虚拟方法在同一个项目的Asset类中被覆盖。这里SerializeBinary()方法将对象转换为二进制流,DeserializeFromBinary()方法与SerializeBinary()相反。

如何编写测试类以便使用Moq进行比较?

我从here:

开始

这是我的代码的一部分:

Streaming上课:

public abstract class Streaming
{
        private static int _streamingIDExponent = 41;
        public const string MonthLookup = "ABCDEFGHIJK";
        public const string ExpiryStringFormat = "ddMMMyyyy";
        public const double DefaultTicksPerPoint = 3;
        private long _StreamingID;
        private int _minQty = DefaultMinimumQty;

        public virtual void SerializeBinary(BinaryStreamWriter binaryStreamWriter)
        {
                 binaryStreamWriter.Write(_StreamingID);
                 binaryStreamWriter.Write(_ex_StreamingID);
                 binaryStreamWriter.Write(_minQty);
                 binaryStreamWriter.Write(_extendedProperties.Count);

                 foreach (KeyValuePair<StreamingPropertyName, StreamingProperty> dictionaryEntry in _extendedProperties)
                 {
                     dictionaryEntry.Value.SerializeBinary(binaryStreamWriter);
                 }
}

public virtual bool DeserializeFromBinary(BinaryStreamReader binaryStreamReader, out string errorString)
{
         errorString = string.Empty;

         try
         {
               _StreamingID = binaryStreamReader.ReadInt64();
               _exStreamingID = binaryStreamReader.ReadInt64();
               _minQty = binaryStreamReader.ReadInt32();    
         }
         catch (Exception oEx)
         {
               errorString = oEx.Message;
         }

         return string.IsNullOrEmpty(errorString);
}

Asset上课:

public class Asset : Streaming
{
    public override void SerializeBinary(BinaryStreamWriter binaryStreamWriter)
    {
        base.SerializeBinary(binaryStreamWriter);
    }

    public override bool DeserializeFromBinary(BinaryStreamReader binaryStreamReader, out string errorString)
    {
            if (!base.DeserializeFromBinary(binaryStreamReader, out errorString))
                   return false;

            try
            {
                  return true;
            }
            catch (Exception oEx)
            {
                 errorString = oEx.Message;
                 return false;
            }
     }
 }

2 个答案:

答案 0 :(得分:1)

你可以像这样创建一个新的流媒体类模拟:

var streamingMock = new Mock<Streaming> { CallBase = true };

调用库很重要,因为它将在您的具体类中执行已实现的代码。 然后,您可以通过Object属性调用方法:

streamingMock.Object.SerializeBinary(...);

希望这有帮助

答案 1 :(得分:1)

在当前的实现中,没有好的方法可以测试AssetStreaming之间与Moq的交互。但是,如果您愿意稍微改变类的实现,那么您可以完成它。基本上,您希望将Streaming类方法的逻辑移动到新方法中,然后您可以模拟它们。

public abstract class Streaming
{
    public virtual void SerializeBinaryCore(BinaryStreamWriter writer)
    {
        // put the logic from your original SerializeBinary method here...
    }

    public virtual bool DeserializeFromBinaryCore(BinaryStreamReader reader, out string errorMessage)
    {
        // put the logic from your original DeserializeFromBinary method here...
    }

    public abstract void SerializeBinary(BinaryStreamWriter writer);

    public abstract bool DeserializeFromBinary(BinaryStreamReader reader, out string errorMessage);
}

然后按如下方式调整您的Asset课程:

public class Asset : Streaming
{
    public override void SerializeBinary(BinaryStreamWriter writer)
    {
        SerializeBinaryCore(writer);
    }
    public override void DeserializeFromBinary(BinaryStreamReader reader, out string errorMessage)
    {
        var result = DeserializeFromBinaryCore(reader, out errorMessage);
        // put the rest of your Asset deserialization logic here...
    }
}

在测试中,您需要创建Mock<Asset> { CallBase = true },然后为SerializeBinaryCoreDeserializeFromBinaryCore方法创建设置。