FakeItEasy 在 C# 中的服务引用

问题描述

我有一个服务引用,其中包含我需要在测试中使用的方法

服务引用类定义为:

public class MyServiceReference : Clientbase<IMyServiceReference>,IMyServiceReference 
{
   public MyServiceReference()
   {
   }

    ..... methods is then defined
}

从我的测试方法来看,我都试过了

private MyServiceReference myServiceReferenceFake = A.Fake<MyServiceReference>();
// And
private MyServiceReference myServiceReference = new MyServiceReference();

对于这两种情况,构造函数都崩溃并显示消息:

system.invalidOperationException: Could not find default endpoint element that references contract.

我所需要的只是从该类中的方法中获得调用定义。 怎么解决

解决方法

我对 Clientbase 没有经验,我假设它是 System.ServiceModel.ClientBase<TChannel>,但我可以发表一些一般性评论。

由于您首先尝试伪造 MyServiceReference,因此我假设您不是在测试该类,而是希望将其用作被测系统的协作者。在这种情况下,最好的办法是尝试伪造 IMyServiceReference。接口很容易伪造,因为它们不会像伪造类那样带来任何行为或包袱。

如果你觉得你真的需要伪造一个 MyServiceReference,那么我们必须面对这样一个事实:FakeItEasy 最终会调用 MyServiceReference(),而 ClientBase<IMyServiceReference>() 会调用 ClientBase<TChannel>,谁的documentation

使用应用程序配置文件中的默认目标端点初始化 MyServiceReference 类的新实例。

根据您报告的错误,我假设未找到应用程序配置文件或不包含创建 MyServiceReference 所需的配置。当您直接尝试实例化 IMyServiceReference 时会遇到同样的错误这一事实加强了我的信念。

所以我认为您的前进道路要么尝试伪造 ClientBase<IMyServiceReference>,要么提供 DownloadManager.Request mediaDownloadRequests = new DownloadManager.Request(Uri.parse(sDownloadableFileURL)) .setTitle(mContext.getResources().getString(R.string.app_name))// Title of the Download Notification .setDescription(sDownloadingMsg)// Description of the Download Notification .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)// Visibility of the download Notification .setDestinationUri(Uri.fromFile(zip_mmpk_file)) // Uri of the destination file .setAllowedOverMetered(true)// Set if download is allowed on Mobile network .setAllowedOverRoaming(true)// Set if download is allowed on roaming network .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE); downloadManager= (DownloadManager) mContext.getSystemService(mContext.DOWNLOAD_SERVICE); long downloadID = downloadManager.enqueue(mediaDownloadRequests)); Log.e("Downloading For --> ","URL : "+sDownloadableFileURL + " || Download ID : "downloadID); 需要的配置。