简体   繁体   English

如何在Xamarin.Forms DependencyService中使用MOQ

[英]How do I use MOQ with Xamarin.Forms DependencyService

I am writing a Xamarin.Forms project which I am now trying to Unit Test currently I use the Xamarin.Forms DependencyService like so: 我正在编写一个Xamarin.Forms项目,我现在尝试Unit Test目前我使用Xamarin.Forms DependencyService,如下所示:

PCL Interface PCL接口

public interface IGetDatabase
{
   string GetDataBase()
}

Device specific Implementation 设备特定实施

[assembly: Dependency(typeof(MyProject.Droid.GetDatabaseImplementation))]
class GetDatabaseImplementation: IGetDatabase
{
   public string GetDatabase()
   {
       return "MyDatabasePath";
   }
}

And it is called in the PCL like so: PCL中调用它是这样的:

DependencyService.Get<IGetDatabase>().GetDatabase();

Now I would like to unit Test this using MOQ to Mock my interface implementation so that my implementations are generated at runtime. 现在我想使用MOQ对我的接口实现进行unit Test以便我的实现在运行时生成。 I don't want to write a mock class as my actual example is more complicated so means it wont work. 我不想写一个模拟类,因为我的实际例子更复杂,所以意味着它不会工作。

How would I go about doing this? 我该怎么做呢? Is my DepdencyService too tightly coupled to Xamarin ? 我的DepdencyService是否与Xamarin紧密耦合?

Unfortunately you can only register a class that implements an interface in the current application. 遗憾的是,您只能在当前应用程序中注册实现接口的类。 You need a dependency injection framework that allows you to register 您需要一个允许您注册的依赖注入框架

a) an object instance or a)对象实例或

b) a method that creates and returns a new mock b)创建并返回新模拟的方法

as the implementation for an interface. 作为接口的实现。

There are many different dependency injection containers for C# available. C#有许多不同的依赖注入容器可用。 I use Mvx which comes with MvvmCross. 我使用Mvx和MvvmCross一起使用。 It allows you to register mocks created wit Moq . 它允许您注册Moq创建的Moq

Example

var myMoq = new Moq<IGetDatabase>();
Moq.Setup(x => x.GetDatabase()).Returns(() => "MyMockDatabasePath");
Mvx.RegisterSingleton<IGetDatabase>(() => myMoq.Object);

暂无
暂无

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

相关问题 Xamarin.Forms:使用DependencyService启动活动 - Xamarin.Forms: Start an activity using DependencyService Xamarin.Forms DependencyService始终返回null - Xamarin.Forms DependencyService always returns null 如何使用 DependencyService 和接口委托将 xamarin.android 特定功能的方法传递给 xamarin.forms? - How to pass a method of xamarin.android specific functionnality to a xamarin.forms using DependencyService and a delegate of Interface? 如何使用 Xamarin.Forms 中的 DependencyService 在共享代码中获取平台特定代码的 static 成员? - How to get static member of platform specific code in shared code using DependencyService in Xamarin.Forms? Xamarin.Forms可移植类中的C#DependencyService - C# DependencyService in Xamarin.Forms portable class Xamarins.Essentials 和 Xamarin.Forms DependencyService 有什么区别 - What is the difference between Xamarins.Essentials and Xamarin.Forms DependencyService Xamarin.Forms DependencyService Get()方法在PCL中返回Null - Xamarin.Forms DependencyService Get() Method Returns Null in PCL 如何在xamarin.forms中将ViewModels绑定到bottomTabbed Viewpages? - How do I use bind Viewmodels to bottomTabbed Viewpages in xamarin.forms? 如何使用 MVVM 和 Xamarin.Forms ListView ItemSelected 事件来检索所选项目的绑定属性? - How do I use MVVM and Xamarin.Forms ListView ItemSelected event to to retrieve a bound property of the item selected? 如何在Xamarin.Forms条目中仅允许数字? - How Do I Allow Only Number In a Xamarin.Forms Entry?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM