Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

57 lines
1.7 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace Microsoft.Web.Mvc.Test
{
public class DynamicReflectionObjectTest
{
[Fact]
public void NoPropertiesThrows()
{
// Arrange
dynamic dro = DynamicReflectionObject.Wrap(new { });
// Act & Assert
Assert.Throws<InvalidOperationException>(
() => dro.baz,
"The property baz doesn't exist. There are no public properties on this object.");
}
[Fact]
public void UnknownPropertyThrows()
{
// Arrange
dynamic dro = DynamicReflectionObject.Wrap(new { foo = 3.4, biff = "Two", bar = 1 });
// Act & Assert
Assert.Throws<InvalidOperationException>(
() => dro.baz,
"The property baz doesn't exist. Supported properties are: bar, biff, foo.");
}
[Fact]
public void CanAccessProperties()
{
// Arrange
dynamic dro = DynamicReflectionObject.Wrap(new { foo = "Hello world!", bar = 42 });
// Act & Assert
Assert.Equal("Hello world!", dro.foo);
Assert.Equal(42, dro.bar);
}
[Fact]
public void CanAccessNestedAnonymousProperties()
{
// Arrange
dynamic dro = DynamicReflectionObject.Wrap(new { foo = new { bar = "Hello world!" } });
// Act & Assert
Assert.Equal("Hello world!", dro.foo.bar);
}
}
}