jeudi 6 février 2020

Using xUnit inlineData when a parameter is a long string

I am trying to convert 4 almost identical testcases into one, where the one instead uses xUnits InlineData.

The thing is that the parameter, which I want to differ is a long string - actually a JSON-"string" with small changes. It goes like this (I've only taken the relevant):

....
....
        var mockHttp = new Mock<HttpMessageHandler>(MockBehavior.Strict);

        mockHttp
            .Protected()
            .Setup<Task<HttpResponseMessage>>(
                "SendAsync",
                ItExpr.IsAny<HttpRequestMessage>(),
                ItExpr.IsAny<CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content = new StringContent( // the below part is the only thing that differs
                    "{'content':[" +
                        "{'Id':'00001', 'eNumber':'001', 'isOwner': true, 'isAlone':false}," +
                        "{'Id':'00001', 'eNumber':'no', 'isOwner': false, 'isAlone':false}," +
                        "{'Id':'00001', 'eNumber':'no', 'isOwner': false, 'isAlone':false}," +
                        "{'Id':'00001', 'eNumber':'no', 'isOwner': false, 'isAlone':false}," +
                        "]}"
                    )
            })
            .Verifiable();

        var httpClient = new HttpClient(mockHttp.Object)
        {
            BaseAddress = new Uri("http://something.com/")
        };

The next test contains the same, but with another 'content', that goes like this:

....
....
                   "{'content':[" +
                    "{'Id':'00001', 'eNumber':'no', 'isOwner': false, 'isAlone':false}," +
                    "{'Id':'00001', 'eNumber':'002', 'isOwner': false, 'isAlone':true}," +
                    "{'Id':'00001', 'eNumber':'no', 'isOwner': false, 'isAlone':false}," +
                    "{'Id':'00001', 'eNumber':'no', 'isOwner': false, 'isAlone':false}," +
                    "]}"
....
....

and a third goes like this:

....
....
                   "{'content':[" +
                    "{'Id':'00001', 'eNumber':'no', 'isOwner': false, 'isAlone':false}," +
                    "{'Id':'00001', 'eNumber':'no', 'isOwner': false, 'isAlone':true}," +
                    "{'Id':'00001', 'eNumber':'007', 'isOwner': true, 'isAlone':true}," +
                    "{'Id':'00001', 'eNumber':'003', 'isOwner': false, 'isAlone':false}," +
                    "]}"
....
....

and so forth with the two next. So I am wondering if there is a smart way to put the 5 types of contents inside a InlineData as a parameter - and thereby somehow save a lot of copied code? There probably is, but I still havent figured out how, without actually writing a lot of code anyway.

Writing the tests as 5 individual tests all goes green, so the only thing I am looking for is a smarter way to write the code, making it cleaner and easier to read, by only changing the parameter/string inside StringContent.

Aucun commentaire:

Enregistrer un commentaire