What is the mocking framework of choice in Unit Te

2020-02-10 02:39发布

When I write unit tests I like to use Rhino Mocks.

So when I started my first Windows Store application I naturally started with my unit tests first. When I tried to add RhinoMocks via NuGet I recieved the following error

Could not install package 'RhinoMocks 3.6.1'. You are trying to install this package into a project that targets '.NETCore,Version=v4.5', but the package does not contain any assembly references that are compatible with that framework. For more information, contact the package author.

I had the same issue with Moq.

Is there a mocking framework for .NETCor,Version=v4.5?

5条回答
在下西门庆
2楼-- · 2020-02-10 03:25

Here are my suggestions, be aware I haven't tested any of these myself yet. Here is a link to an article discussing the first two options.

  1. TypeMock Isolator - expensive, but looks like it would do the job
  2. Microsoft Fakes (evolved from a project at MSR called Moles)
  3. Instead of mocking (or doing dependency injection), you could use a different technique. You could use parameterized injection and just pass in the information you need to test. This reduces to large objects to delegates and primitives, relieving the need for a mocking framework. Here is more about this, including an example of the technique in action.
查看更多
放荡不羁爱自由
3楼-- · 2020-02-10 03:34

Most mocking frameworks are based on Reflection.Emit. Unfortunately Reflection.Emit isn't in WinRT. This means you can't do dynamic proxies. (I.e. Run-time mocking). This leaves pre-generation of mocks that get referenced at compile-time. The only framework I know of is an experimental branch of Moq: https://github.com/mbrit/moqrt

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-02-10 03:36

I know the original question is old but there are many of us still out there trying to accomplish mocking in Windows Store Apps.

I have recently started to use MoqaLate which states: Mocking for Windows Phone & Windows Store apps. It actually physically creates the MockingClasses after you Build your solution. So, after build you will have a folder with some Mock implementation of your classes.

I think is not the best option. Run-time mocking will be the ideal but in the meantime it creates the mocks for me.

查看更多
欢心
5楼-- · 2020-02-10 03:43

Actually, Philipp Dolder, one of the contributors to FakeItEasy has come up with an interesting and working approach based on Portable Class Libraries. http://www.planetgeek.ch/2013/02/01/fakeiteasy-and-windows-store-apps-are-becoming-friends/

In essence, he proposes the following solution:

  1. Put all your productive code that will be TDD-ed into a PCL class library where you select whatever target frameworks you like to support
  2. Create your test assembly as a regular class library
  3. Add the references to FakeItEasy and your test framework(s) of choice (e.g. NUnit + FluentAssertions) to your *.Test assembly
  4. Only put the UI stuff that you can’t test into a Windows Store App project
查看更多
男人必须洒脱
6楼-- · 2020-02-10 03:46

I made this for use with Windows Phone 7, should work with Win 8 as it's just a post build step:

http://dontcodetired.com/blog/post/Introducing-%28probably%29-The-Worlds-Only-Mocking-Framework-for-Windows-Phone-7-%28WP7%29.aspx

查看更多
登录 后发表回答