コード例 #1
0
ファイル: DeepObjectGraphTest.cs プロジェクト: Hengle/Jil
        public void DeepObjectGraphShouldNotBeEmpty()
        {
            // given
            var model = new UserCredentialsUpdateResponse()
            {
                User =
                    new UserDto()
                {
                    DateOfBirth  = new DateTime(1980, 01, 01),
                    CreatedAtUtc = new DateTime(2014, 01, 01, 08, 00, 00),
                    UserId       = 1,
                    Username     = "******"
                }
            };

            //When
            var stringBuilder = new StringBuilder();

            using (var stringOutput = new StringWriter(stringBuilder))
            {
                JSON.Serialize(model, stringOutput, Options.IncludeInherited);
            }
            var serializedJsonAsString = stringBuilder.ToString();

            // round tripping
            var deserializedRoundtrippedObject   = JSON.Deserialize(serializedJsonAsString, typeof(UserCredentialsUpdateResponse), Options.IncludeInherited);
            var deserializedRoundtrippedInstance = deserializedRoundtrippedObject as UserCredentialsUpdateResponse;

            // Then
            Assert.NotEqual("{}", serializedJsonAsString);
            Assert.NotNull(deserializedRoundtrippedInstance);
            Assert.IsType <UserCredentialsUpdateResponse>(deserializedRoundtrippedInstance);

            Assert.NotNull(deserializedRoundtrippedInstance.User); // << this fails

            Assert.Equal(deserializedRoundtrippedInstance.User.CreatedAtUtc.ToUniversalTime(), model.User.CreatedAtUtc.ToUniversalTime());
            Assert.Equal(deserializedRoundtrippedInstance.User.DateOfBirth.ToUniversalTime(), model.User.DateOfBirth.ToUniversalTime());
            Assert.Equal(deserializedRoundtrippedInstance.User.UserId, model.User.UserId);
            Assert.Equal(deserializedRoundtrippedInstance.User.Username, model.User.Username);
        }
コード例 #2
0
        public void DeepObjectGraphShouldNotBeEmpty()
        {
            // given
            var model = new UserCredentialsUpdateResponse()
            {
                User =
                    new UserDto()
                    {
                        DateOfBirth = new DateTime(1980, 01, 01),
                        CreatedAtUtc = new DateTime(2014, 01, 01, 08, 00, 00),
                        UserId = 1,
                        Username = "******"
                    }
            };

            //When
            var stringBuilder = new StringBuilder();
            using (var stringOutput = new StringWriter(stringBuilder))
            {
                JSON.Serialize(model, stringOutput, Options.IncludeInherited);
            }
            var serializedJsonAsString = stringBuilder.ToString();

            // round tripping
            var deserializedRoundtrippedObject = JSON.Deserialize(serializedJsonAsString, typeof(UserCredentialsUpdateResponse), Options.IncludeInherited);
            var deserializedRoundtrippedInstance = deserializedRoundtrippedObject as UserCredentialsUpdateResponse;

            // Then
            Assert.AreNotEqual("{}", serializedJsonAsString);
            Assert.IsNotNull(deserializedRoundtrippedInstance);
            Assert.IsInstanceOfType(deserializedRoundtrippedInstance, typeof(UserCredentialsUpdateResponse));

            Assert.IsNotNull(deserializedRoundtrippedInstance.User); // << this fails

            Assert.AreEqual(deserializedRoundtrippedInstance.User.CreatedAtUtc.ToUniversalTime(), model.User.CreatedAtUtc.ToUniversalTime());
            Assert.AreEqual(deserializedRoundtrippedInstance.User.DateOfBirth.ToUniversalTime(), model.User.DateOfBirth.ToUniversalTime());
            Assert.AreEqual(deserializedRoundtrippedInstance.User.UserId, model.User.UserId);
            Assert.AreEqual(deserializedRoundtrippedInstance.User.Username, model.User.Username);
        }