Add unit test to show JsonUtil handling IList<T> types
This commit is contained in:
parent
53ab54d7b1
commit
48ae58196d
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Blazor.Test
|
||||
|
|
@ -47,12 +48,13 @@ namespace Microsoft.AspNetCore.Blazor.Test
|
|||
Id = 1844,
|
||||
Name = "Athos",
|
||||
Pets = new[] { "Aramis", "Porthos", "D'Artagnan" },
|
||||
Hobby = Hobbies.Swordfighting
|
||||
Hobby = Hobbies.Swordfighting,
|
||||
Nicknames = new List<string> { "Comte de la Fère", "Armand" }
|
||||
};
|
||||
|
||||
// Act/Assert
|
||||
Assert.Equal(
|
||||
"{\"Id\":1844,\"Name\":\"Athos\",\"Pets\":[\"Aramis\",\"Porthos\",\"D'Artagnan\"],\"Hobby\":2}",
|
||||
"{\"Id\":1844,\"Name\":\"Athos\",\"Pets\":[\"Aramis\",\"Porthos\",\"D'Artagnan\"],\"Hobby\":2,\"Nicknames\":[\"Comte de la Fère\",\"Armand\"]}",
|
||||
JsonUtil.Serialize(person));
|
||||
}
|
||||
|
||||
|
|
@ -60,7 +62,7 @@ namespace Microsoft.AspNetCore.Blazor.Test
|
|||
public void CanDeserializeClassFromJson()
|
||||
{
|
||||
// Arrange
|
||||
var json = "{\"Id\":1844,\"Name\":\"Athos\",\"Pets\":[\"Aramis\",\"Porthos\",\"D'Artagnan\"],\"Hobby\":2}";
|
||||
var json = "{\"Id\":1844,\"Name\":\"Athos\",\"Pets\":[\"Aramis\",\"Porthos\",\"D'Artagnan\"],\"Hobby\":2,\"Nicknames\":[\"Comte de la Fère\",\"Armand\"]}";
|
||||
|
||||
// Act
|
||||
var person = JsonUtil.Deserialize<Person>(json);
|
||||
|
|
@ -70,6 +72,7 @@ namespace Microsoft.AspNetCore.Blazor.Test
|
|||
Assert.Equal("Athos", person.Name);
|
||||
Assert.Equal(new[] { "Aramis", "Porthos", "D'Artagnan" }, person.Pets);
|
||||
Assert.Equal(Hobbies.Swordfighting, person.Hobby);
|
||||
Assert.Equal(new[] { "Comte de la Fère", "Armand" }, person.Nicknames);
|
||||
}
|
||||
|
||||
class Person
|
||||
|
|
@ -78,6 +81,7 @@ namespace Microsoft.AspNetCore.Blazor.Test
|
|||
public string Name { get; set; }
|
||||
public string[] Pets { get; set; }
|
||||
public Hobbies Hobby { get; set; }
|
||||
public IList<string> Nicknames { get; set; }
|
||||
}
|
||||
|
||||
enum Hobbies { Reading = 1, Swordfighting = 2 }
|
||||
|
|
|
|||
Loading…
Reference in New Issue