Пример #1
0
        public void Setup()
        {
            _mockEvent  = new ManualResetEvent(true);
            _mockResult = new Mock <IAsyncResult>();
            _mockResult.SetupGet(r => r.AsyncWaitHandle).Returns(_mockEvent);

            _mockSocket = new Mock <ISocket>();

            _mockBeginConnect = _mockSocket.Setup(s => s.BeginConnect(It.IsAny <System.Net.EndPoint>(), It.IsAny <AsyncCallback>(), It.IsAny <object>()));
            _mockBeginConnect.Returns(_mockResult.Object);

            _mockBeginReceive = _mockSocket.Setup(s => s.BeginReceive(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <SocketFlags>(), It.IsAny <AsyncCallback>(), It.IsAny <object>()));
            _mockEndReceive   = _mockSocket.Setup(s => s.EndReceive(It.IsAny <IAsyncResult>()));
            _mockConnected    = _mockSocket.SetupGet(s => s.Connected);
            _mockConnected.Returns(true);

            _mockBeginReceive.Callback <byte[], int, int, SocketFlags, AsyncCallback, object>((b, offset, s, f, c, o) =>
            {
                _sendMessage?.CopyTo(b, 0);

                _receiveCallback = c;
            });

            _sendMessage     = null;
            _receiveCallback = null;
        }
Пример #2
0
        private DefaultRpcInvoker GetInvoker(MethodInfo?methodInfo, RpcPath?path       = null,
                                             Action <RpcServerConfiguration>?configure = null)

        {
            var        logger         = new Mock <ILogger <DefaultRpcInvoker> >(MockBehavior.Loose);
            var        options        = new Mock <IOptions <RpcServerConfiguration> >(MockBehavior.Strict);
            var        matcher        = new Mock <IRpcRequestMatcher>(MockBehavior.Strict);
            var        accessor       = new Mock <IRpcContextAccessor>(MockBehavior.Strict);
            RpcContext requestContext = this.GetRouteContext(path);

            accessor
            .Setup(a => a.Get())
            .Returns(requestContext);
            Moq.Language.Flow.ISetup <IRpcRequestMatcher, IRpcMethodInfo> matcherSetup = matcher
                                                                                         .Setup(m => m.GetMatchingMethod(It.IsAny <RpcRequestSignature>()));
            if (methodInfo != null)
            {
                //TODO better way of getting this for unit tests?
                DefaultRpcMethodInfo method = DefaultRpcMethodInfo.FromMethodInfo(methodInfo);
                matcherSetup.Returns(method);
            }
            else
            {
                matcherSetup.Throws(new RpcException(RpcErrorCode.MethodNotFound, "Method not found"));
            }
            var config = new RpcServerConfiguration();

            config.ShowServerExceptions = true;
            configure?.Invoke(config);
            options
            .SetupGet(o => o.Value)
            .Returns(config);
            var authHandler = new Mock <IRpcAuthorizationHandler>(MockBehavior.Strict);

            authHandler
            .Setup(h => h.IsAuthorizedAsync(It.IsAny <IRpcMethodInfo>()))
            .Returns(Task.FromResult(true));

            return(new DefaultRpcInvoker(logger.Object, options.Object, matcher.Object, accessor.Object, authHandler.Object));
        }
 public static void ReturnsInOrder <T, TResult>(
     this Moq.Language.Flow.ISetup <T, TResult> setup,
     params TResult[] results) where T : class
 {
     setup.Returns(new Queue <TResult>(results).Dequeue);
 }