Search code examples
unit-testingxamarinnunitmissingmethodexceptionxamarin-test-cloud

Using Reflection Causing MissingMethodException in Xamarin UITest


I have a class in my Xamarin PCL which makes a call to System.Reflection.GetRuntimeProperties. For an example, let's say my PCL class has this method:

public string ExampleMethod(string arg) {
    if(arg == null) return null;
    IEnumerable<PropertyInfo> infos = this.GetType().GetRuntimeProperties();
    return infos[0].Name;
}

I then have a Xamarin.UITest project which references the PCL project and tests this class. I have two test cases in my TestFixture so far, which for our example would be the following:

    [Test]
    public void TestExampleMethod_ArgNull_Null(){
        Assert.That (exampleInstance.ExampleMethod(null), Is.Null);
    } 

    [Test]
    public void TestExampleMethod_ArgNotNull_NotNull(){
        Assert.That (exampleInstance.ExampleMethod("testValue"), Is.NotNull);
    } 

When I run the Xamarin.UITest project, it compiles, runs the tests, and completes fine on both Android and iOS platforms. The TestExampleMethod_ArgNull_Null test passes since it returns early. However, the TestExampleMethod_ArgNotNull_NotNull test fails with:

System.MissingMethodException : Method 'RuntimeReflectionExtensions.GetRuntimeProperties' not found.

So it appears that even though everything is compiling just fine, and I am able to run other test cases fine, the Xamarin.UITest project is not able to use anything in System.Reflection. Does anyone know how I go about debugging this?


Solution

  • I posted this to Xamarin Support as well (thanks @jgoldberger) and we were able to figure out that it was due to a project setup issue. This is a project which uses Couchbase Lite which requires a specific version of Json.Net (6.0.4 as of this post). I must have accidentally updated the packages on some of the projects since the same version of Json.Net was not being used across all the projects (PCL, Android, iOS, and UITest). I ended up re-creating the project from scratch and that took care of it.