11

I'm having trouble unit testing ASP.NET Core MVC controllers that return anonymous objects. The unit testing is set up in a separate project and calls the controller methods from the main project directly.

The controller methods return IActionResult but typically these are OkObjectResult and BadRequestObjectResult objects that get translated into a JSON response with the appropriate HTTP status code. The anonymous objects are passed as the constructor parameters for the ObjectResult objects and it is these I'm trying to make assertions against (accessible via ObjectResult.Value).

I found this question (how can i access internals in asp.net 5) that has an answer that says to use dynamics and add

[assembly: InternalsVisibleTo("Namespace")]

to AssemblyInfo.cs to allow the test project access to the internal object properties of the anonymous objects. However, latest versions of ASP.NET Core MVC do not have AssemblyInfo.cs and adding one as suggested in the answers to the linked question does not work either.

Is there now a different location to add the InternalsVisibleTo or am I missing something?

1

1 Answer 1

24

Original idea from this answer with a more generic approach. Using a custom DynamicObject as a wrapper for inspecting the value via reflection there was no need to add the InternalsVisibleTo

public class DynamicObjectResultValue : DynamicObject, IEquatable<DynamicObjectResultValue> {
    private readonly object value;

    public DynamicObjectResultValue(object value) {
        this.value = value;
    }

    #region Operators
    public static bool operator ==(DynamicObjectResultValue a, DynamicObjectResultValue b) {
        // If both are null, or both are same instance, return true.
        if (System.Object.ReferenceEquals(a, b)) {
            return true;
        }
        // If one is null, but not both, return false.
        if (ReferenceEquals((object)a, null) || ReferenceEquals((object)b, null)) {
            return false;
        }
        // Return true if the fields match:
        return a.value == b.value;
    }

    public static bool operator !=(DynamicObjectResultValue a, DynamicObjectResultValue b) {
        return !(a == b);
    }
    #endregion

    public override IEnumerable<string> GetDynamicMemberNames() {
        return value.GetType().GetProperties().Select(p => p.Name);
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result) {
        //initialize value
        result = null;
        //Search possible matches and get its value
        var property = value.GetType().GetProperty(binder.Name);
        if (property != null) {
            // If the property is found, 
            // set the value parameter and return true. 
            var propertyValue = property.GetValue(value, null);
            result = propertyValue;
            return true;
        }
        // Otherwise, return false. 
        return false;
    }

    public override bool Equals(object obj) {
        if (obj is DynamicObjectResultValue)
            return Equals(obj as DynamicObjectResultValue);
        // If parameter is null return false.
        if (ReferenceEquals(obj, null)) return false;
        // Return true if the fields match:
        return this.value == obj;
    }

    public bool Equals(DynamicObjectResultValue other) {
        // If parameter is null return false.
        if (ReferenceEquals(other, null)) return false;
        // Return true if the fields match:
        return this.value == other.value;
    }

    public override int GetHashCode() {
        return ToString().GetHashCode();
    }

    public override string ToString() {
        return string.Format("{0}", value);
    }
}

Assuming the following controller

public class FooController : Controller {

    public IActionResult GetAnonymousObject() {

        var jsonResult = new {
            id = 1,
            name = "Foo",
            type = "Bar"
        };

        return Ok(jsonResult);
    }

    public IActionResult GetAnonymousCollection() {

        var jsonResult = Enumerable.Range(1, 20).Select(x => new {
            id = x,
            name = "Foo" + x,
            type = "Bar" + x
        }).ToList();

        return Ok(jsonResult);
    }
}

Tests could look like

[TestMethod]
public void TestDynamicResults() {
    //Arrange
    var controller = new FooController();

    //Act
    var result = controller.GetAnonymousObject() as OkObjectResult;

    //Assert
    dynamic obj = new DynamicObjectResultValue(result.Value);

    Assert.IsNotNull(obj);
    Assert.AreEqual(1, obj.id);
    Assert.AreEqual("Foo", obj.name);
    Assert.AreEqual(3, obj.name.Length);
    Assert.AreEqual("Bar", obj.type);
}

[TestMethod]
public void TestDynamicCollection() {
    //Arrange
    var controller = new FooController();

    //Act
    var result = controller.GetAnonymousCollection() as OkObjectResult;

    //Assert
    Assert.IsNotNull(result, "No ActionResult returned from action method.");
    dynamic jsonCollection = result.Value;
    foreach (dynamic value in jsonCollection) {
        dynamic json = new DynamicObjectResultValue(value);

        Assert.IsNotNull(json.id,
            "JSON record does not contain \"id\" required property.");
        Assert.IsNotNull(json.name,
            "JSON record does not contain \"name\" required property.");
        Assert.IsNotNull(json.type,
            "JSON record does not contain \"type\" required property.");
    }
}
5
  • This is a really elegant solution for ASP.NET Core MVC projects that no longer use AssemblyInfo. Works perfectly so I've accepted it as the answer.
    – Jargon
    Jul 19, 2016 at 9:44
  • This is awesome! Gives me 97% of what I need :) Now, about that 3%... Is it possible to get the length of jsonCollection? and what would it take to get, say, jsonCollection[0], so I can assert not just NotNull, but also the actual value: Assert.Equal(4, jsonCollection.Count()) and Assert.Equal(25, jsonCollection[0].id). If you want me to start a new thread - happy to do that!
    – Felix
    Jul 23, 2016 at 20:53
  • 1
    @Felix Add the thread and let me know. I've been working on refining this work around since putting it here. When I get a chance I'll take a look at the details in your comment. off the top of my head. for jsonCollection[0] I'm thinking override TryGetIndex. for the asserts your have to cast because you are dealing with dynamic ie Assert.Equal(4, (int)jsonCollection.Count()). I've been trying to do it like you but so far no dice. casting seemed to work so I stuck with that.
    – Nkosi
    Jul 23, 2016 at 20:58
  • done. thank you in advance :) stackoverflow.com/questions/38546914/…
    – Felix
    Jul 23, 2016 at 22:21
  • 1
    @Jargon. expanded the class a bit for a similar question. See if this would also be of use to you.
    – Nkosi
    Jul 24, 2016 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.