如何使用 XUnit `InlineData` 以便为 MultipartFormDataContent 插入多个测试数据?
How do I use XUnit `InlineData` so that I can insert multiple test data for MultipartFormDataContent?
我有这个有效的 Xunit 代码。
[Fact]
public async Task When_DocumentTypeInvalidFileType_Then_ShouldFail()
{
using (var client = new HttpClient())
{
var filePath = @"D:\Files\test.pdf";
using (var stream = File.OpenRead(filePath))
{
using (var form = new MultipartFormDataContent())
{
form.Add(new StringContent(Guid.NewGuid().ToString()), "Id");
var file = new StreamContent(stream);
file.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
form.Add(file, "File", Path.GetFileName(filePath));
// Act
var response = await client.PostAsync("/ABC/Document", form);
var responseString = await response.Content.ReadAsStringAsync();
_output.WriteLine("response: {0}", responseString);
HttpStatusCode statusCode = response.StatusCode;
Assert.Equal(HttpStatusCode.BadRequest, statusCode);
var result = JsonConvert.DeserializeObject<ResponseMessage>(responseString);
Assert.Equal("Invalid file type.", result.file[0]);
}
}
}
但以上仅针对一种情况进行测试。我如何使用 XUnit InlineData
以便我可以为 MultipartFormDataContent
插入多个测试数据?
[Theory]
[InlineData(MediaTypeNames.Application.Pdf, @"D:\Files\test.pdf")]
[InlineData(MediaTypeNames.Application.Xml, @"D:\Files\test.xml")]
// else files what you need
public async Task When_DocumentTypeInvalidFileType_Then_ShouldFail(string contentType, string filePath)
{
using (var client = new HttpClient())
{
using (var stream = File.OpenRead(filePath))
{
using (var form = new MultipartFormDataContent())
{
form.Add(new StringContent(Guid.NewGuid().ToString()), "Id");
var file = new StreamContent(stream);
file.Headers.ContentType = new MediaTypeHeaderValue(contentType);
form.Add(file, "File", Path.GetFileName(filePath));
// Act
var response = await client.PostAsync("/ABC/Document", form);
var responseString = await response.Content.ReadAsStringAsync();
_output.WriteLine("response: {0}", responseString);
HttpStatusCode statusCode = response.StatusCode;
Assert.Equal(HttpStatusCode.BadRequest, statusCode);
var result = JsonConvert.DeserializeObject<ResponseMessage>(responseString);
Assert.Equal("Invalid file type.", result.file[0]);
}
}
}
}
或
[Theory]
[MemberData(nameof(TestGenerator.GetTestData), MemberType = typeof(TestGenerator))]
public async Task When_DocumentTypeInvalidFileType_Then_ShouldFail(string contentType, string filePath)
{
using (var client = new HttpClient())
{
using (var stream = File.OpenRead(filePath))
{
using (var form = new MultipartFormDataContent())
{
form.Add(new StringContent(Guid.NewGuid().ToString()), "Id");
var file = new StreamContent(stream);
file.Headers.ContentType = new MediaTypeHeaderValue(contentType);
form.Add(file, "File", Path.GetFileName(filePath));
// Act
var response = await client.PostAsync("/ABC/Document", form);
var responseString = await response.Content.ReadAsStringAsync();
_output.WriteLine("response: {0}", responseString);
HttpStatusCode statusCode = response.StatusCode;
Assert.Equal(HttpStatusCode.BadRequest, statusCode);
var result = JsonConvert.DeserializeObject<ResponseMessage>(responseString);
Assert.Equal("Invalid file type.", result.file[0]);
}
}
}
}
class TestGenerator
{
public static IEnumerable<object[]> GetTestData() => new List<object[]>
{
new object[] { MediaTypeNames.Application.Pdf, @"D:\Files\test.pdf" },
new object[] { MediaTypeNames.Application.Xml, @"D:\Files\test.xml" }
};
}
我有这个有效的 Xunit 代码。
[Fact]
public async Task When_DocumentTypeInvalidFileType_Then_ShouldFail()
{
using (var client = new HttpClient())
{
var filePath = @"D:\Files\test.pdf";
using (var stream = File.OpenRead(filePath))
{
using (var form = new MultipartFormDataContent())
{
form.Add(new StringContent(Guid.NewGuid().ToString()), "Id");
var file = new StreamContent(stream);
file.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
form.Add(file, "File", Path.GetFileName(filePath));
// Act
var response = await client.PostAsync("/ABC/Document", form);
var responseString = await response.Content.ReadAsStringAsync();
_output.WriteLine("response: {0}", responseString);
HttpStatusCode statusCode = response.StatusCode;
Assert.Equal(HttpStatusCode.BadRequest, statusCode);
var result = JsonConvert.DeserializeObject<ResponseMessage>(responseString);
Assert.Equal("Invalid file type.", result.file[0]);
}
}
}
但以上仅针对一种情况进行测试。我如何使用 XUnit InlineData
以便我可以为 MultipartFormDataContent
插入多个测试数据?
[Theory]
[InlineData(MediaTypeNames.Application.Pdf, @"D:\Files\test.pdf")]
[InlineData(MediaTypeNames.Application.Xml, @"D:\Files\test.xml")]
// else files what you need
public async Task When_DocumentTypeInvalidFileType_Then_ShouldFail(string contentType, string filePath)
{
using (var client = new HttpClient())
{
using (var stream = File.OpenRead(filePath))
{
using (var form = new MultipartFormDataContent())
{
form.Add(new StringContent(Guid.NewGuid().ToString()), "Id");
var file = new StreamContent(stream);
file.Headers.ContentType = new MediaTypeHeaderValue(contentType);
form.Add(file, "File", Path.GetFileName(filePath));
// Act
var response = await client.PostAsync("/ABC/Document", form);
var responseString = await response.Content.ReadAsStringAsync();
_output.WriteLine("response: {0}", responseString);
HttpStatusCode statusCode = response.StatusCode;
Assert.Equal(HttpStatusCode.BadRequest, statusCode);
var result = JsonConvert.DeserializeObject<ResponseMessage>(responseString);
Assert.Equal("Invalid file type.", result.file[0]);
}
}
}
}
或
[Theory]
[MemberData(nameof(TestGenerator.GetTestData), MemberType = typeof(TestGenerator))]
public async Task When_DocumentTypeInvalidFileType_Then_ShouldFail(string contentType, string filePath)
{
using (var client = new HttpClient())
{
using (var stream = File.OpenRead(filePath))
{
using (var form = new MultipartFormDataContent())
{
form.Add(new StringContent(Guid.NewGuid().ToString()), "Id");
var file = new StreamContent(stream);
file.Headers.ContentType = new MediaTypeHeaderValue(contentType);
form.Add(file, "File", Path.GetFileName(filePath));
// Act
var response = await client.PostAsync("/ABC/Document", form);
var responseString = await response.Content.ReadAsStringAsync();
_output.WriteLine("response: {0}", responseString);
HttpStatusCode statusCode = response.StatusCode;
Assert.Equal(HttpStatusCode.BadRequest, statusCode);
var result = JsonConvert.DeserializeObject<ResponseMessage>(responseString);
Assert.Equal("Invalid file type.", result.file[0]);
}
}
}
}
class TestGenerator
{
public static IEnumerable<object[]> GetTestData() => new List<object[]>
{
new object[] { MediaTypeNames.Application.Pdf, @"D:\Files\test.pdf" },
new object[] { MediaTypeNames.Application.Xml, @"D:\Files\test.xml" }
};
}