0

我正在使用 MVC 架构构建一个 Web 应用程序。我将需要使用仍在开发中的 Web 服务(我们遵循敏捷方法)。网络服务有几种方法。几种方法是稳定的(已发布和运行),有些方法仍在开发中。

所以这意味着,我需要从客户端模拟新方法(直到它们准备好)并继续使用旧方法(用于回归测试)。

在方法级别模拟服务的最佳实践是什么?欢迎任何建议或想法。我可以使用任何模拟框架吗?

我将在 ASP.Net MVC 框架以及基于 CodeIgniter 的 PHP 应用程序上应用它。提前致谢。

4

2 回答 2

0

可能有很多方法可以做到这一点。这就是我所做的。它可能属于也可能不属于“最佳实践”类别。

我编写了一个带有 Web 服务接口的包装器。

假设我们的 WebService 有四个方法,Get(), Create(), Update(), Delete()

我的界面很简单

public interface IServiceWrapper
{
  object Get();
  object Create(object toCreate);
  object Update(object toUpdate);
  bool Delete(object toDelete);
}

现在我可以有两个实现。调用实际网络服务的一种

public class ServiceWrapper : IServiceWrapper
{
  public object Get(){//call webservice Get()}
  public object Create(object toCreate){//call webservice Create()}
  public object Update(object toUpdate){//call webservice Update()}
  public bool Delete(object toDelete){//call webservice Delete()}
}

还有一个假(或模拟)实现,我在其中模仿 Web 服务行为(通常使用内存数据)

public class FakeServiceWrapper : IServiceWrapper
{
  private void PopulateObjects()
  {
     //mimic your data source here if you are not using moq or some other mocking framework
  }
  public object Get(){//mimic behavior of webservice Get()}
  public object Create(object toCreate){//mimic behavior of webservice Create()}
  public object Update(object toUpdate){//mimic behavior of webservice Update()}
  public bool Delete(object toDelete){//mimic behavior of webservice Delete()}
}

通常,我会通过将实例注入消费服务或控制器来使用其中一个或另一个。但是,如果您愿意,您可以轻松地实例化每个包装器的实例并在方法级别“挑选”。

于 2013-03-06T17:50:18.477 回答
0

由于我使用的是依赖注入,我们无法在方法级别在 Mock 和 Real Service 之间切换。换句话说,我们需要使用MockRealTime服务。

参考上面四十二的例子,

我将在IServiceWrapperfrom的 Mock 或 Real 实现之间切换RegisterUnityMapping

在我的开发团队中,这是一种可行的方法。在本地开发环境中,当我有时切换到 Mock 来运行几个单元测试时 - 否则,总是使用真正的实现。不用说,在更高的环境中 - 只使用 Real 实现。

索姆

于 2013-06-10T19:41:18.887 回答