简体   繁体   English

如何使用Visual Studio单元测试框架或NUnit框架在C#中模拟对象?

[英]How to mock objects in C# using Visual Studio Unit Testing framework or NUnit framework?

I have moved to developing applications in C# from Java. 我已经开始从Java用C#开发应用程序。 I came across the Visual Studio unit testing framework provided by Visual Studio, and another framework called NUnit. 我遇到了Visual Studio提供的Visual Studio单元测试框架,以及另一个称为NUnit的框架。

  1. Which of these frameworks support mocking objects, or something similar to returning specific response to a method in another project whose source code is not available? 这些框架中的哪个框架支持模拟对象,或者类似于对另一个源代码不可用的项目中的方法返回特定响应的东西?

I'm using Visual Studio Pro, so Microsoft Fakes is not applicable to me. 我使用的是Visual Studio Pro,因此Microsoft假货不适用于我。 Also, I came across moq, and rhinomocks but I'm not sure about these. 另外,我遇​​到了最小起订量和犀牛,但是我不确定这些。

  1. I was wondering if I can use a unit testing framework like the one provided by Visual Studio or NUnit without having to add additional dependency. 我想知道是否可以使用像Visual Studio或NUnit提供的那样的单元测试框架,而不必添加其他依赖项。

There are three popular unit testing frameworks used to support unit testing (Microsft Microsoft.VisualStudio.TestTools , NUnit and xUnit ) and none of them supports mocking. 有三种流行的单元测试框架用于支持单元测试(Microsft Microsoft.VisualStudio.TestToolsNUnitxUnit ),但它们都不支持模拟。

From my experience, moq works quite well for most projects. 根据我的经验, moq对于大多数项目都非常有效。 For the example of how to use it in practice check this project DjvuNet . 有关如何在实践中使用它的示例,请检查此项目DjvuNet There you will find multiple examples of simple and complex mocking scenarios. 在那里,您将找到简单和复杂的模拟场景的多个示例。 This particular project uses xUnit as a unit test framework but it does not have any impact on how mocking is done. 这个特定的项目使用xUnit作为单元测试框架,但是它对模拟的完成方式没有任何影响。

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

相关问题 Visual Studio的C#单元测试框架? - C# Unit Test framework for Visual Studio? 使用受保护对象C#编写单元测试的好方法是什么(使用NMock和NUnit框架) - What's a good way to write Unit tests for code with protected objects C# (using NMock, and NUnit framework) 使用Visual Studio单元测试框架比较两个对象 - Comparing two objects with Visual Studio Unit Testing Framework 如何以编程方式退出正在运行的单元测试(使用Visual Studio c#和单元测试框架) - How to exit a running unit test programmatically (with Visual Studio c#, and unit test framework) 我可以使用Visual Studio单元测试框架强制对属性进行单元测试吗? - Can I force unit testing of properties using the Visual Studio unit testing framework? C#:不使用第3方框架的单元测试? - C# : Unit testing without using 3rd party framework? C#Visual Studio中的单元测试 - Unit-testing in C# Visual Studio 如何在 c# 中模拟用于单元测试的记录器? - How to mock a logger for unit testing in c#? 单元测试使用NUnit和C#的异步方法 - Unit Testing Async Methods Using NUnit and C# 如何使用C#自动化框架依次执行两个服务(使用C#,NUnit,Specflow技术) - How to Execute two services sequential using c# automation framework(Using technologies C#, NUnit, Specflow)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM