English 中文(简体)
rhino mocks: how tobuilding a fake socket?
原标题:rhino mocks: how to build a fake socket?

我试图利用以下法典为测试建立一个假体:

var socket = MockRepository.GenerateStub<Socket>(
    AddressFamily.InterNetwork, 
    SocketType.Stream, ProtocolType.IP
);
socket.Stub(v => v.RemoteEndPoint).PropertyBehavior().Return(
    new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345)
);

然而,试图给我带来以下例外:

发出了最后呼吁,或者没有发出呼吁(确保你采用虚拟(C#)/可逆(VB)方法)。

谁能帮助我发现它会错失的地方?

增 编

最佳回答

Thx Rup。

我以我为Socket撰写一个包裹,把所有必要的方法作为虚拟方法,来解决这个问题。

public class SocketWrapper
{
    private readonly Socket _socket;

    public SocketWrapper(Socket socket)
    {
        _socket = socket;
    }

    public virtual EndPoint RemoteEndPoint
    {
        get { return _socket.RemoteEndPoint; }
    }

    public virtual void Close()
    {
        _socket.Close();
    }

    public virtual void EndDisconnect(IAsyncResult asyncResult)
    {
        _socket.EndDisconnect(asyncResult);
    }

    public virtual bool Connected
    {
        get { return _socket.Connected; }
    }

    public virtual int Send(byte[] data)
    {
        return _socket.Send(data);
    }

    public virtual IAsyncResult BeginReceive(byte[] buffer, int offset, int size, SocketFlags flags, AsyncCallback callback, object state )
    {
        return _socket.BeginReceive(buffer, offset, size, flags, callback, state);
    }

    public virtual int EndReceive(IAsyncResult asyncResutl)
    {
        return _socket.EndReceive(asyncResutl);
    }

    public virtual IAsyncResult BeginDisconnect(bool reuseSocket,AsyncCallback callback, object state)
    {
        return _socket.BeginDisconnect(reuseSocket, callback, state);
    }
}
问题回答

我的错误在哪里?

错误信息似乎只是自我解释。 你们只能使用模拟虚拟方法。 在你的案件中,你试图篡改RemoteEndPoint,但该财产并非虚拟=>不可能 mo。 此外,为抽象的班级/接口制造 mo也是明智的。 在您的案例中,你试图篡改不可能的Socket级。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签