模拟服务器以模拟不存在的服务 [英] Mocking server to mock nonexistent service

查看:53
本文介绍了模拟服务器以模拟不存在的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要某种免费的强大模拟服务器,因为我们创建了前端的一部分,我们稍后将创建Web API,所以现在我们需要一个工具来帮助我们模拟不存在的服务。我们尝试使用 Apiary ,但它并不像我们预期的那样对我们有效,因为它有时会删除请求/响应内容。任何人都可以给我建议,比如 Apiary 吗?



最好的问候,

Amr Muhammad



我尝试过:



尝试使用 Apiary 模拟服务器工具。

I need some sort of a free robust mocking server as we created part of the front-end and we are going to create the Web API later so for now we need a facility to help us mock the nonexistent service. we tried to use Apiary but it did not work for us as we expect as it sometimes removes the request/response content. Can anyone help by give me suggestion like Apiary in that?

Best Regards,
Amr Muhammad

What I have tried:

Tried using Apiary mocking server facility.

推荐答案

传统上,您将使用接口来实现代码与之交互的虚拟服务,以后可以用工作代替。



You would traditionally use interfaces to implement a dummy service your code interacts with which can later be replaced by a working one.

public interface IMyService
{
    void SomeMethod(int id);
    bool SomeOtherMethod(string value);
}





对于测试,您只需模拟服务





For testing you simply mock the service

/// <summary>
/// This is a mocked version of the service
/// </summary>
public class MockedMyService : IMyService
{
    public void SomeMethod(int id)
    {

    }

    public bool SomeOtherMethod(string value)
    {
        return true;
    }
}





当实际api可用时你会写一个合适的版本使用它





when the actual api is available you'll write a proper version that uses it

/// <summary>
/// This version of the service will communicate with the finished API
/// </summary>
public class MyService : IMyService
{
    public void SomeMethod(int id)
    {
        // implement code
    }

    public bool SomeOtherMethod(string value)
    {
        // implement code
    }
}





客户端代码然后在接口级别工作,并使用依赖注入来决定是否使用模拟版本或真实版本。





The client code then works at the interface level and you use dependency injection to decide if the mocked version or the real version is used.

public class MyClient
{
    private IMyService myService;

    // use dependency injection to configure which concrete class is passed to the constructor
    public MyClient(IMyService myService)
    {
        this.myService = myService;
    }

    public bool DoSomething()
    {
        myService.SomeMethod(123);

        return myService.SomeOtherMethod("hello world");
    }
}


这篇关于模拟服务器以模拟不存在的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆