protected override void EstablishContext()
        {
            controlDevice = A.Fake <ISpiControlDevice>();
            controlDevice
            .CallsTo(device => device.Control(A <uint> .Ignored, A <SpiTransferControlStructure[]> .Ignored))
            .Returns(IOCTL_PINVOKE_RESULT_CODE);

            connection = new NativeSpiConnection(controlDevice);

            // SPI control structure we expect to see during the P/Invoke call
            controlStructure = new SpiTransferControlStructure {
                BitsPerWord      = BITS_PER_WORD,
                Length           = 5,
                Delay            = DELAY,
                ChipSelectChange = 1,
                Speed            = SPEED_IN_HZ
            };

            buffer = A.Fake <ISpiTransferBuffer>();
            buffer
            .CallsTo(b => b.ControlStructure)
            .Returns(controlStructure);

            // setup fake collection to return our "prepared" fake buffer
            collection = A.Fake <ISpiTransferBufferCollection>();
            collection
            .CallsTo(c => c.Length)
            .Returns(1);
            collection
            .CallsTo(c => c.GetEnumerator())
            .ReturnsLazily(call => new List <ISpiTransferBuffer> {
                buffer
            }.GetEnumerator());
        }
        protected override void EstablishContext()
        {
            controlDeviceMock = new Mock <ISpiControlDevice>();
            controlDevice     = controlDeviceMock.Object;
            controlDeviceMock.Setup(x => x.Control(It.IsAny <uint>(), ref It.Ref <SpiTransferControlStructure> .IsAny)).Returns(IOCTL_PINVOKE_RESULT_CODE);

            connection = new NativeSpiConnection(controlDevice);

            // SPI control structure we expect to see during the P/Invoke call
            controlStructure = new SpiTransferControlStructure
            {
                BitsPerWord      = BITS_PER_WORD,
                Length           = 5,
                Delay            = DELAY,
                ChipSelectChange = 1,
                Speed            = SPEED_IN_HZ
            };

            bufferMock = new Mock <ISpiTransferBuffer>();
            buffer     = bufferMock.Object;
            bufferMock.Setup(x => x.ControlStructure).Returns(controlStructure);

            // setup fake collection to return our "prepared" fake buffer
            collectionMock = new Mock <ISpiTransferBufferCollection>();
            collection     = collectionMock.Object;
            collectionMock.Setup(x => x.Length).Returns(1);
            collectionMock.Setup(x => x.GetEnumerator()).Returns(new List <ISpiTransferBuffer> {
                buffer
            }.GetEnumerator());
        }
        /// <summary>
        /// Starts the SPI data transfer.
        /// </summary>
        /// <param name="transferBuffers">The transfer buffers that contain data to be send and/or the received data.</param>
        /// <returns>An <see cref="int"/> that contains the result of the transfer operation.</returns>
        public int Transfer(ISpiTransferBufferCollection transferBuffers)
        {
            if (transferBuffers == null)
            {
                throw new ArgumentNullException("transferBuffers");
            }

            var request = Interop.Interop.GetSpiMessageRequest(transferBuffers.Length);

            var structures = transferBuffers
                             .Select(buf => buf.ControlStructure)
                             .ToArray();
            var result = deviceFile.Control(request, structures);

            result.ThrowOnPInvokeError <SendSpiMessageException>("Can't send SPI messages. Error {1}: {2}");

            return(result);
        }
        /// <summary>
        /// Starts the SPI data transfer.
        /// </summary>
        /// <param name="transferBuffers">The transfer buffers that contain data to be send and/or the received data.</param>
        /// <returns>An <see cref="int"/> that contains the result of the transfer operation.</returns>
        public int Transfer(ISpiTransferBufferCollection transferBuffers)
        {
            if (transferBuffers == null) {
                throw new ArgumentNullException("transferBuffers");
            }

            var request = Interop.GetSpiMessageRequest(transferBuffers.Length);

            var structures = transferBuffers
                .Select(buf => buf.ControlStructure)
                .ToArray();
            var result = deviceFile.Control(request, structures);

            result.ThrowOnPInvokeError<SendSpiMessageException>("Can't send SPI messages. Error {1}: {2}");

            return result;
        }
        protected override void EstablishContext()
        {
            controlDevice = A.Fake<ISpiControlDevice>();
            controlDevice
                .CallsTo(device => device.Control(A<uint>.Ignored, A<SpiTransferControlStructure[]>.Ignored))
                .Returns(IOCTL_PINVOKE_RESULT_CODE);

            connection = new NativeSpiConnection(controlDevice);

            // SPI control structure we expect to see during the P/Invoke call
            controlStructure = new SpiTransferControlStructure {
                BitsPerWord = BITS_PER_WORD,
                Length = 5,
                Delay = DELAY,
                ChipSelectChange = 1,
                Speed = SPEED_IN_HZ
            };

            buffer = A.Fake<ISpiTransferBuffer>();
            buffer
                .CallsTo(b => b.ControlStructure)
                .Returns(controlStructure);

            // setup fake collection to return our "prepared" fake buffer
            collection = A.Fake<ISpiTransferBufferCollection>();
            collection
                .CallsTo(c => c.Length)
                .Returns(1);
            collection
                .CallsTo(c => c.GetEnumerator())
                .ReturnsLazily(call => new List<ISpiTransferBuffer>{buffer}.GetEnumerator());
        }