简体   繁体   English

如何使用NUnit DynamicMock模拟非接口和非MarshalByRef

[英]How To use NUnit DynamicMock to mock non-Interface and non-MarshalByRef

i am writing a Class wich will be initialised with a Socket . 我正在编写一个将使用Socket初始化的类。 I want to write a Test using an NUnit DynamicMock. 我想使用NUnit DynamicMock编写测试。

How can i create a Mock of the Socket without much effort? 如何在不费力的情况下创建插槽模拟?

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 编辑#1

I looked into moq nad it looks quite nice however i still can not test. 我看着moq nad它看起来很不错但是我仍然无法测试。 My initial Problem was to find the rigth version to use, but i think i solved this. 我最初的问题是找到要使用的rigth版本,但我认为我解决了这个问题。

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

The Problem ist that Socket does not feature a Constructor without parameters. 问题是Socket没有没有参数的构造函数。 Now i do not want to give it a parameter, i want to mock everything. 现在我不想给它一个参数,我想模仿一切。

Edit #2 This seems to be a problem for a lot of people The target is to create a mock directly from the socket. 编辑#2这似乎是很多人的问题目标是直接从套接字创建一个模拟。

I think mocking Socket is a fairly advanced task NUnit DynamicMock isn't suited for that, after all they don't pretend to be a real powerful mocking solution. 我认为模拟Socket是一个相当高级的任务NUnit DynamicMock不适合它,毕竟他们并没有假装是一个真正强大的模拟解决方案。

I am personally using Moq, and since Socket class isn't sealed I think mocking it with Moq should be pretty straightforward and suitable for your needs. 我个人使用Moq,因为Socket类没有密封,我认为用Moq嘲笑它应该非常简单,适合你的需要。

Nunit DynamicMock isn't very well documented and represented in internet it appears, but I took a look here into code from its constructor Nunit DynamicMock没有很好的文档记录,并在互联网上出现,但我在这里看了一下构造函数的代码

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

looks like it isn't supposed to work with anything except interfaces, so you need to look into real mock framework once you need that. 看起来它不应该使用除接口之外的任何东西,所以你需要在需要时查看真正的模拟框架。

I resolved my issue. 我解决了我的问题。

The answer is DynamicMock just with Interfaces. 答案是使用Interfaces的DynamicMock。

Moq just with prosperparameters or, since i want it easy a lot of times - here is what i did: Moq只是与繁荣的参数,或者,因为我想要很容易很多次 - 这就是我做的:

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 the test: 在设置测试时:

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

i can not think you can get it with less effort than this. 我不认为你可以用这么少的努力来获得它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM