我正在尝试使用Moles来模拟System.Net.Sockets中的Socket类。我已经成功地生成了.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;
}
}
}一个愚蠢的测试类:
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>有人能指出这有什么问题吗?该怎么做才能让它工作呢?我知道还有其他方法可以模拟Socket类,例如使用实现我自己的接口的包装类,但我只对使用Moles来做这件事感兴趣。
--谢谢,丹尼尔
发布于 2012-02-21 02:31:26
根据我的经验,打磨系统组装是很奇怪的。在.moles文件的第二行尝试这样做:
<Assembly Name="System" ReflectionOnly="true" />https://stackoverflow.com/questions/9366347
复制相似问题