molesを使用してSystem.net.socketsをmockします
-
28-10-2019 - |
質問
Molesを使用して、System.net.Socketsのソケットクラスをmockしようとしています。 .molesファイルを生成することに成功しました。これにより、system.net.molesアセンブリを参照に追加しましたが、MSocketクラスは生成されません。実際、MipendPointCollectionクラスのみが生成されます。
System.net.Sockets.Socketを使用するサンプルクラスは次のとおりです。
using System.Text;
using System.Net.Sockets;
namespace MyProject
{
public static class Communicator
{
public static int Send(string messages)
{
byte[] bytes = Encoding.ASCII.GetBytes(messages);
int bytesSent = 0;
using (Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp))
{
socket.Connect("localhost", 1234);
bytesSent = socket.Send(bytes);
}
return bytesSent;
}
}
}
Dumy Testクラス:
using MyProject;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Net.Moles;
using Microsoft.Moles.Framework;
[assembly: MoledType(typeof(System.Net.Sockets.Socket))]
namespace MyProjectTest
{
[TestClass()]
public class CommunicatorTest
{
private TestContext testContextInstance;
public TestContext TestContext
{
get { return testContextInstance; }
set { testContextInstance = value; }
}
[TestMethod()]
[HostType("Moles")]
public void SendTest()
{
//dose not exist in the System.Net.Moles namespace:
//MSocket.Send = deligate(){ return 0; };
int bytesSent = Communicator.Send("Test Message");
Assert.AreEqual(0, bytesSent);
}
}
}
そして私の.molesファイル:
<Moles xmlns="http://schemas.microsoft.com/moles/2010/">
<Assembly Name="System.Net" />
</Moles>
誰かがこれで何が悪いのか、それを機能させるために何をすべきかを指摘できますか。たとえば、自分のインターフェイスを実装するラッパークラスでソケットクラスを模倣する他の方法があることは知っていますが、ほくろを使用してこれを行うことに興味があります。
- ダニエルに感謝します
解決
私の経験では、システムアセンブリの脱皮は風変わりです。 .molesファイルの2行目でこれを試してください:
<Assembly Name="System" ReflectionOnly="true" />
所属していません StackOverflow