2008-12-08 13 views
3

我想以一種測試驅動的方式在C#下使用Remoting開始,但是我陷入了困境。C#中的Testdriven Remoting - 我需要一個單獨的服務器AppDomain嗎?

一兩件事,我的話題是發現這個article by Marc Clifton,但他似乎從控制檯手動啓動它有服務器上運行。

我嘗試讓服務器在測試夾具中啓動(即註冊服務類)。 我可能也有錯誤的接口的用法,但那會晚一些。

我總是得到該通道已經註冊的異常(遺憾的德國消息)。 System.Runtime.Remoting.RemotingException:Der Channel tcp wurde bereits registriert。

註釋掉ChannelServices.RegisterChannell在試驗方法()行之後,它發生用於呼叫Activator.GetObject()。

我試圖把StartServer()放到一個線程中,但是這也沒有幫助。 我發現創建一個新的AppDomain可能是一種可能的方式,但還沒有嘗試過。

你能告訴我,如果我的方法本質上是錯誤的?我該如何解決它?

using System; 
using NUnit.Framework; 
using System.Runtime.Remoting; 
using System.Runtime.Remoting.Channels; 
using System.Runtime.Remoting.Channels.Tcp; 

namespace Bla.Tests.Remote 
{ 
    [TestFixture] 
    public class VerySimpleProxyTest 
    { 
     int port = 8082; 
     string proxyUri = "MyRemoteProxy"; 
     string host = "localhost"; 

     IChannel channel; 

     [SetUp] 
     public void SetUp() 
     { 
      StartServer(); 
     } 

     [TearDown] 
     public void TearDown() 
     { 
      StopServer(); 
     } 

     [Test] 
     public void UseRemoteService() 
     { 
      //IChannel clientChannel = new TcpClientChannel(); 
      //ChannelServices.RegisterChannel(clientChannel, false); 
      string uri = String.Format("tcp://{0}:{1}/{2}", host, port, proxyUri); 
      IMyTestService remoteService = (IMyTestService)Activator.GetObject(typeof(IMyTestService), uri); 

      Assert.IsTrue(remoteService.Ping()); 
      //ChannelServices.UnregisterChannel(clientChannel); 
     } 

     private void StartServer() 
     { 
      channel = new TcpServerChannel(port); 
      ChannelServices.RegisterChannel(channel, false); 
      RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyTestService), proxyUri, WellKnownObjectMode.Singleton); 
     } 

     private void StopServer() 
     { 
      ChannelServices.UnregisterChannel(channel); 
     } 
    } 

    public interface IMyTestService 
    { 
     bool Ping(); 
    } 

    public class MyTestService : MarshalByRefObject, IMyTestService 
    { 
     public bool Ping() 
     { 
      return true; 
     } 
    } 
} 

回答

0

我發現了一個很好的方法來完成我想要做的事情,只是使用WCF而不是Remoting。

我在移植到the article by Yair Cohen給出NUnit的源代碼不到5分鐘內,它工作的開箱。

1

我真的沒有解決你的問題,但我的建議是,不要以這種方式編寫單元測試。請參閱post。你真的想在這裏測試哪些代碼。我很確定微軟已經做了很多測試.net附帶的Remoting功能。實現您的Service接口的類可以通過剛剛完成的實現進行單元測試。如果.net框架沒有使用靜態的註冊位,註冊你的服務接口的代碼將是可測試的,但可惜。您可以嘗試將IChannel模擬傳遞給ChannelServices.RegisterChannel,並以某種方式驗證您的註冊碼,但在我看來,這會浪費時間。

我只是想指出,測試應該是達到目的的一種手段,但不是目的本身。

+0

測試驅動並不僅僅意味着你知道的單元測試,它可以包括集成測試和交易的端到端測試。這可能會讓TDD的人哭,但除了單元測試之外,我傾向於在單元測試工具中進行WCF集成測試,啓動WCF主機,發出消息並測試響應。這當然不是浪費時間。 – blowdart 2009-05-27 21:02:54

相關問題