Mocking framework for use with xamarin android

后端 未结 3 1088
[愿得一人]
[愿得一人] 2020-12-20 04:44

I am trying to find a mocking framework that works with Xamarin Android. So far I have tried Rhino Mocks and Moq but both depend on System.Web.dll which does not seem to be

相关标签:
3条回答
  • 2020-12-20 05:06

    From what I've observed, there really is no mocking frameworks that work well with Xamarin. On the iOS side, this is mainly due to the inability to use Reflection.Emit because of the code generation restriction imposed by Apple. I realize you're on Android, so that restriction does not apply to you. However, other restrictions arise, like you've found with RhinoMocks. My favorite mocking framework is Moq, but I can't use it with Xamarin because of some of the configuration namespaces that are implemented in .NET, but not in Mono.

    So, your best bet is just manual mocks. I know that's not the answer you want to hear, but it seems to be the consensus.

    0 讨论(0)
  • 2020-12-20 05:06

    I've been using a mock generator called PCLMock. I had to fork it to get it working with .netstandard, you can give it a go by cloning it and building it from here: https://github.com/pellet/PCLMock

    It works by generating code before compile time, therefore not needing the reflection apis missing in AOT compiled xamarin projects.

    Otherwise you could always go functional style and pass in your dependencies as delegates/Funcs, this way you don't need to generate mocks for interfaces, you can use a builder class for injecting mocked out lambdas in unit tests.

    0 讨论(0)
  • 2020-12-20 05:09

    You can use True Fakes (http://truefakes.net) mocking framework to solve your issues. It doesn’t use Reflection.Emit at runtime, but it automatically generates mocks at compile time and add it to assembly where unit-tests are. This allows you to use True Fakes for writing unit tests with fully isolated system under test for Xamarin.Android and Xamarin.iOS and running them directly on the device!

    You can add it to your project via NuGet (https://www.nuget.org/packages/TrueFakes/). Please see more information about True Fakes at http://truefakes.net.

    PS. I’m developer of True Fakes mocking framework.

    0 讨论(0)
提交回复
热议问题