2015-06-30 14 views
1

我嘗試單元測試protobuf-net配置,不幸的是,事實證明RuntimeTypeModel.Default上沒有Clear方法(或類似的方法)。 由於我在整個應用程序中使用RuntimeTypeModel.Default,我不想切換到RuntimeTypeModel.Create()如何重置protobuf-net RuntimeTypeModel.Default模型?

有沒有一種方法來重置/清除/刪除RuntimeTypeModel.DefaultAdd方法添加的類型?

+0

你有沒有設法找到一種方法來做到這一點?我還發現,一旦您將類型註冊爲代理,您不能重新添加它們;例如在單元測試類的情況下。 – cmdel

回答

0

我有一個xUnit測試類似的問題。構造函數將爲每個測試實例化,並且RuntimeTypeModel已經具有代理類型並且失敗。該解決方案是使用ClassFixture中的xUnit(我相信這是對的TestFixture NUnit的?)

using System; 
using System.Collections.Generic; 
using System.IO; 
using NodaTime; 
using ProtoBuf.Meta; 
using Quantum.Serialisation; 
using Quantum.Serialisation.Surrogates; 
using Xunit; 

namespace Quantum.SerialisationTests 
{ 

    public class TestFixture : IDisposable 
    { 
     private RuntimeTypeModel model; 

     public TestFixture() 
     { 
      RuntimeTypeModel.Create(); 
      model = RuntimeTypeModel.Default; 
      model.Add(typeof (ZonedDateTimeSurrogate), true); 
      model.Add(typeof (ZonedDateTime), false).SetSurrogate(typeof (ZonedDateTimeSurrogate)); 
     } 

     public void Dispose() 
     { 
     } 
    } 

    public class ProtobufTests : IClassFixture<TestFixture> 
    { 

     public ProtobufTests() 
     { 
     } 

     // Tests go here 

     public void SetFixture(TestFixture data) 
     { 
     } 
    } 
} 

不管你有多少測試必須在ProtobufTests規範代孕只會被添加一次。