How to use NUnit DynamicMock to mock without interface and without marshalByRef

I am writing a class that will initialize Socket . I want to write a test using NUnit DynamicMock.

How can I create a Socket layout effortlessly?

DynamicMock DynamicSocketMock = new DynamicMock(typeof (Socket)); 
/*No ERROR - BUT I THINK HERE IS THE PROBLEM*/

Socket SocketMock = (Socket) DynamicSocketMock.MockInstance;
/*RUNTIME ERROR*/

ToBeTested Actual = new ToBeTested(SocketMock);
/*NEVER REACHED*/

      

Edit # 1

I looked in mok-n-n but looked pretty good but I still can't check. My original problem was to find the version for the version, but I think I solved that.

var Mock = new Mock<Socket>();
Socket MockSocket = (Socket)Mock.Object;
ToBeTested Actual = new ToBeTested(SocketMock);

      

The problem is that Socket does not have a parameterless constructor. Now I don't want to give it a parameter, I want to mock all the time.

Edit # 2 This seems to be a problem for a lot of people The goal is to create the layout directly from the socket.

0


source to share


2 answers


I think ridicule Socket

is a pretty difficult task. NUnit DynamicMock is not the right fit for this, as they are not pretending to be a real powerful mocking solution.

I personally use Moq and since the Socket class is not sealed, I think mocking it with Moq should be pretty simple and suitable for your needs.

Nunit DynamicMock is not well documented and presented on the internet, but I looked at the code here from my constructor



http://nunit.sourcearchive.com/documentation/2.5.10.11092plus-pdfsg-1/DynamicMock_8cs_source.html#l00014

it looks like it shouldn't work with anything other than interfaces, so you need to look into the real fake structure when you need it.

+2


source


I solved the problem.

The answer is DynamicMock with interfaces only.

Moq just with prosperparameters or since I want it to be easy many times - this is what I did:

public class Test_MockSocket : Socket
{
    public Test_MockSocket() : base (
                    (new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30000)).AddressFamily,
                    (SocketType.Stream),
                    (ProtocolType.Tcp))
    {
    }
}

      



And when setting up a test:

        var Mock = new Mock<Test_MockSocket>();
        Socket MockSocket = (Socket)Mock.Object;
        ToBeTested Actual = new ToBeTested (MockSocket);

      

I cannot think that you can get it with less effort than this.

0


source







All Articles