在Xamarin UITest中使用反射导致MissingMethodException

时间:2015-07-22 21:08:05

标签: unit-testing xamarin nunit missingmethodexception xamarin-test-cloud

我的Xamarin PCL中有一个类,它调用System.Reflection.GetRuntimeProperties。举个例子,假设我的PCL类有这个方法:

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

然后我有一个Xamarin.UITest项目,它引用了PCL项目并测试了这个类。到目前为止,我的TestFixture中有两个测试用例,我们的例子如下:

    [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);
    } 

当我运行Xamarin.UITest项目时,它会编译,运行测试,并在Android和iOS平台上完成。 TestExampleMethod_ArgNull_Null测试因为它提前返回而通过。但是,TestExampleMethod_ArgNotNull_NotNull测试失败,并显示:

  

System.MissingMethodException:找不到方法'RuntimeReflectionExtensions.GetRuntimeProperties'。

因此,即使所有内容都编译得很好,并且我能够正常运行其他测试用例,Xamarin.UITest项目也无法在System.Reflection中使用任何内容。有谁知道我如何调试这个?

2 个答案:

答案 0 :(得分:0)

在我的结尾,使用以下内容无法构建:

IEnumerable<PropertyInfo> infos = this.GetType().GetRuntimeProperties();
return infos[0].Name;

由于无法对IEnumerable进行括号索引。我改变了这个:

List<PropertyInfo> infos = this.GetType().GetRuntimeProperties().ToList();
return infos[0].Name;

构建项目并成功运行测试。

使用Reflection的方法的类在PCL中,该PCL是从UI Test项目引用的。

所以基本上我无法重现你得到的错误。

答案 1 :(得分:0)

我也将此发布给Xamarin支持(感谢@jgoldberger),我们能够发现它是由于项目设置问题。这是一个使用Couchbase Lite的项目,它需要特定版本的Json.Net(截至本文的6.0.4)。我必须不小心更新了一些项目的软件包,因为在所有项目(PCL,Android,iOS和UITest)中没有使用相同版本的Json.Net。我最终从头开始重新创建项目,并负责处理它。