Unit Test HTTPRequest Headers with ServiceStack

后端 未结 1 1889
情歌与酒
情歌与酒 2020-12-16 05:16

I have this Service:

public class PlayerService : Service
{
    public IPlayerAppService PlayerAppService { get; set; }

    public PlayerService (IPlayerApp         


        
相关标签:
1条回答
  • 2020-12-16 05:29

    You're using an self-hosting HttpListener as you would for an integration test, but you're not doing in integration test.

    An integration test would look like:

    [Test]
    public void NewPlayer_Should_Return201AndLocation ()
    {
        var client = new JsonServiceClient("http://localhost:1337/") {
            ResponseFilter = httpRes => {
                //Test response headers...
            };
        }
    
        PlayerResponse response = client.Post(new Player { ... });
    }
    

    Otherwise if you want to do an unit test you don't need an AppHost and can just test the PlayerService class just like any other C# class, injecting all the dependencies and the mock Request context it needs.

    [Test]
    public void NewPlayer_Should_Return201AndLocation ()
    {
        var mockCtx = new Mock<IRequestContext>();
        mockCtx.SetupGet (f => f.AbsoluteUri).Returns("localhost:1337/player");
    
        PlayerService service = new PlayerService {
            MyOtherDependencies = new Mock<IMyOtherDeps>().Object,
            RequestContext = mockCtx.Object,
        };
    
        HttpResult response = (HttpResult)service.Post(new Player { ... });
    
        //Assert stuff..
    }
    
    0 讨论(0)
提交回复
热议问题