【问题标题】:Xamarin UI/Unit Tests with Realm使用 Realm 进行 Xamarin UI/单元测试
【发布时间】:2017-02-17 12:20:58
【问题描述】:

我使用 Xamarin.Forms 创建了一个应用,该应用使用 realm 来存储信息。

我想编写一些测试来检查我的应用程序中的注销功能是否正常工作以及领域是否没有数据。

在一个单独的解决方案中,我安装了 Realm 0.82.1 包并创建了对 PCL 解决方案的引用以引用模型以开始测试。

更改模型时,由于需要迁移而引发异常:

public static class RealmHelper {
    public static Realm GetInstance() {
        var config = new RealmConfiguration(Constants.REALM_FILE);
        try {
            return Realm.GetInstance(config);
        } catch (Exception e) {
            Debug.WriteLine(e.Message);
            Realm.DeleteRealm(config); // Error Thrown here
            return Realm.GetInstance(config);
        }
    }
}

然而这个错误被抛出

System.PlatformNotSupportedException : Realm 的 PCL 构建正在 链接,这可能意味着您需要使用 NuGet 或以其他方式链接 特定于平台的 Realm.dll 到您的主应用程序。

我的测试

[Test]
public void CanInitDB() {
    Assert.DoesNotThrow(() =>
    {
        new DataAccessController();
    });
}

我的控制者

public class DataAccessController {
    static Realm realm;

    public DataAccessController() {
        realm = RealmHelper.GetInstance();
    }

    //...
}

【问题讨论】:

标签: xamarin xamarin.forms realm


【解决方案1】:

Realm 不支持使用没有特定平台对应的 PCL 版本。您可以使用Xamarin's built-in test runners 生成一个应用程序,您可以使用该应用程序在设备/模拟器上执行测试。这有点不方便,但应该可以完成工作。

【讨论】:

  • 我实际上已经按照本指南来设置我的测试。但是我仍然收到错误:Expected: No Exception to be thrown But was: (The PCL build of Realm is being linked which probably means you need to use NuGet or otherwise link a platform-specific Realm.dll to your main application.) 如何在测试套件中链接特定于平台的 dll?
  • 你需要从 test runner 项目中引用 Realm nuget。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
  • 2020-12-30
  • 1970-01-01
相关资源
最近更新 更多