Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net C#Asyn。套接字编程_.net_Asp.net_Unit Testing_Sockets - Fatal编程技术网

.net C#Asyn。套接字编程

.net C#Asyn。套接字编程,.net,asp.net,unit-testing,sockets,.net,Asp.net,Unit Testing,Sockets,是否可以对Asyn进行单元测试。套接字编程(使用c#)?提供一些示例单元测试代码。我假设您正在测试自己的某个使用.NET流的类;让我们称之为MessageSender。请注意,没有理由对.NET流本身进行单元测试,这是Microsoft的工作。您不应该对.NET framework代码进行单元测试,而应该自己测试 首先,确保注入MessageSender使用的流。不要在类内部创建它,而是接受它作为属性值或构造函数参数。例如: public sealed class MessageSender {

是否可以对Asyn进行单元测试。套接字编程(使用c#)?提供一些示例单元测试代码。

我假设您正在测试自己的某个使用.NET流的类;让我们称之为
MessageSender
。请注意,没有理由对.NET流本身进行单元测试,这是Microsoft的工作。您不应该对.NET framework代码进行单元测试,而应该自己测试

首先,确保注入
MessageSender使用的流。不要在类内部创建它,而是接受它作为属性值或构造函数参数。例如:

public sealed class MessageSender
{
   private readonly Stream stream;

   public MessageSender(Stream stream)
   {
      if (stream == null)
         throw new ArgumentNullException("stream");
      this.stream = stream;
   }

   public IAsyncResult BeginSendHello(AsyncCallback callback, object state)
   {
      byte[] message = new byte[] {0x01, 0x02, 0x03};
      return this.stream.BeginWrite(
         message, 0, message.Length, callback, state);
   }

   public void EndSendHello(IAsyncResult asyncResult)
   {
      this.stream.EndWrite(asyncResult);
   }
}
现在是一个示例测试:您可以测试
beginsendhollo
在流上调用
BeginWrite
,并发送正确的字节。我们将模拟流并设置一个期望来验证这一点。我在本例中使用的是框架

[Test]
public void BeginSendHelloInvokesBeginWriteWithCorrectBytes()
{
   var mocks = new MockRepository();
   var stream = mocks.StrictMock<Stream>();
   Expect.Call(stream.BeginWrite(
      new byte[] {0x01, 0x02, 0x03}, 0, 3, null, null));
   mocks.ReplayAll();

   var messageSender = new MessageSender(stream);
   messageSender.BeginSendHello(null, null);

   mocks.VerifyAll();
}
[测试]
public void BeginSendHelloInvokesBeginWriteWithCorrectBytes()
{
var mocks=new MockRepository();
var stream=mocks.StrictMock();
Expect.Call(stream.BeginWrite(
新字节[]{0x01,0x02,0x03},0,3,null,null));
mocks.ReplayAll();
var messageSender=新的messageSender(流);
messageSender.beginsendhollo(null,null);
mocks.VerifyAll();
}

@user919426您声称答案是“旧的”,并指向使用套接字而不是流的Microsoft文档。然而,这些只是不同的抽象。您可以在NetworkStream中包装套接字以将其转换为流。socket的文档还建议使用TCPClient,这将为您提供一个流。如果我下面的答案有点过时的话,那就是使用BeginWrite/EndWrite,而不是更现代的WriteAsync.Hi@wimconen。。请不要生气。从某种意义上说,
APM
不再是异步TCP的推荐模式。现在建议使用
async/await
)。Microsoft文档已经过时,并且基于旧的APM模式。我有我的答案。赏金不再有用了。