c# - 进行 WCF 服务调用的单元测试方法?

标签 c# wpf wcf unit-testing automated-tests

第一次尝试任何真正的单元测试。我有一个 WPF 客户端应用程序,它从 WCF 服务中的方法接收数据。这些方法调用是直接从客户端应用程序中的 View 模型进行的:

   public string LoadArticle()
    {
       MyServiceClient msc = new MyServiceClient();
       return Article = msc.GetArticle(_UserName);
    }

然后我有一个测试项目,我在其中新建一个 View 模型然后调用我的方法:

    [TestMethod]
    public void LoadArticleTestValid()
    {
        var articleViewModel = new ArticleViewModel("Thomas");

        string userName = "Thomas";
        string expected = "Article 1";

        var actual = articleViewModel.LoadArticle(userName); 

        etc.           
    }

显然此测试将失败,因为客户端应用程序无法访问服务以调用 LoadArticle。如何解决这种情况?我是使用依赖注入(inject)并将某种 IMyService 接口(interface)传递给构造函数,而不是在 ViewModel 中创建 MyServiceClient,还是以某种方式模拟服务?

最佳答案

问题是:

MyServiceClient msc = new MyServiceClient();

这在 ArticleViewModelMyServiceClient 之间建立了紧密的耦合。为了对 just ArticleViewModel 进行单元测试,需要模拟此依赖项。如果有一个 IMyServiceClient,那么您可以将它提供给类:

public ArticleViewModel
{
    private IMyServiceClient ServiceClient { get; set; }

    public ArticleViewModel(IMyServiceClient serviceClient)
    {
        this.ServiceClient = serviceClient;
    }

    // etc.
}

然后该类中的代码不会创建新的服务客户端,它只会使用该属性中的服务客户端。

然后在单元测试中,您将创建 IMyServiceClient 的模拟,在该模拟上定义预期的行为,并将其提供给被测试的对象。你如何做到这一点取决于模拟库。 Rhino Mocks 中的一个简单示例可能看起来像这样:

// arrange
var serviceClient = MockRepository.GenerateMock<IMyServiceClient>();
serviceClient.Stub(c => c.GetArticle(Arg<string>.Is.Anything)).Return("Article 1");

var model = new ArticleViewModel(serviceClient);

// act
var result = model.LoadArticle("Thomas");

// assert
Assert.AreEqual("Article 1", result);

这里的想法是,单元测试只测试 LoadArticle 方法,而不是该方法调用的依赖项。这些依赖项提供了预期的行为,并根据这些预期的行为检查结果。

注意:没有什么能阻止单元测试提供 MyServiceClient实际实现,而不是模拟。单元测试项目只需要该服务的配置即可工作。 (单元测试项目是应用程序宿主,它们可以有 App.config 文件。)这将是一个集成测试而不是单元测试,但可以对结果进行相同的断言。

关于c# - 进行 WCF 服务调用的单元测试方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28898812/

相关文章:

c# - 带有可选参数的发射函数

c# - Mvvm 取消选中底层 Listbox 对象中的选中框

c# - Ninject - 静态类中的内核?

wpf - 将自定义按钮添加到现有的 MessageBox

c# - WCF WebService 中的 GZip 压缩

c# - IIS 托管 WCF 与 SSL 安全 -"The HTTP request was forbidden with client authentication scheme ' 匿名'"错误

c# - 使 AForge VideoCaptureDevice 与 Unity 一起工作

c# - 如果在 DB2 中超过分配的时间,则终止 SQL 查询执行

c# - 在 C# 中命名 Windows API 常量

c# - 组合 WPF View 和 MVVM View 模型