我正在构建一个使用MVC架构的web应用程序。我将需要使用一个仍在开发中的we服务(我们遵循敏捷方法)。webservice有几个方法。有几个方法是稳定的(发布并运行),一些方法仍在开发中。
因此,这意味着,在客户端,我需要模拟新方法(直到它们准备就绪),并继续使用旧方法(用于回归测试)。
在方法级别模拟服务的最佳实践是什么?欢迎任何建议或想法。我可以使用任何模拟框架吗?
我将在PHP框架以及构建在CodeIgniter上的ASP.Net应用程序上应用这一点。提前谢谢。
发布于 2013-06-11 03:41:19
因为我使用的是依赖注入,所以我们不能在方法级别上在模拟和真实服务之间切换。换句话说,我们将需要使用模拟或 RealTime服务。
参考上面由四十二所示的例子,
我将在RegisterUnityMapping的IServiceWrapper的模拟或真实实现之间切换。
在我的开发团队中,这是一种可行的方法。而在本地开发环境中,当我有时切换到Mock来运行几个单元测试时-否则,总是使用真正的实现。不用说,在更高的环境中-只使用Real实现。
Som
发布于 2013-03-07 01:50:18
可能有很多方法可以做到这一点。这就是我要做的。它可能属于也可能不属于“最佳实践”类别。
我编写了一个带有web服务接口的包装器。
假设我们的WebService有四个方法,Get(),Create(),Update(),Delete()。
我的界面非常简单
public interface IServiceWrapper
{
object Get();
object Create(object toCreate);
object Update(object toUpdate);
bool Delete(object toDelete);
}现在我可以有两个实现。调用实际的webservice的
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()}
}和一个假的(或模拟的)实现,其中我模仿了webservice的行为(通常使用内存数据)。
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()}
}通常,我将通过将实例注入到使用服务或控制器中来使用其中一个。但是如果你愿意,你可以很容易地实例化每个包装器的一个实例,并在方法级别上“挑选”。
https://stackoverflow.com/questions/15253555
复制相似问题