Xamarin iOS中Realm的单元测试

问题描述

Xamarin iOS本机应用程序。我必须为Realm API编写单元测试。它是具有过滤数据能力的层。它可能包含复杂的Linq查询,因此必须进行测试。 例如,方法

Task<List<T>> Get(int count,int type,int? id);

创建NUnit测试我需要获取Realm对象:

var realm = Realm.GetInstance(new InMemoryConfiguration("test"));

但出现异常:

System.TypeInitializationException : The type initializer for 'Realms.SharedRealmHandle' threw an exception.
  ----> System.DllNotFoundException : realm-wrappers assembly:<unkNown assembly> type:<unkNown type> member:(null)
  at Realms.InMemoryConfiguration.CreateRealm (Realms.Schema.RealmSchema schema) [0x0003e] in C:\jenkins\workspace\realm_realm-dotnet_PR\Realm\Realm\Configurations\InMemoryConfiguration.cs:62 
  at Realms.Realm.GetInstance (Realms.RealmConfigurationBase config,Realms.Schema.RealmSchema schema) [0x0003c] in C:\jenkins\workspace\realm_realm-dotnet_PR\Realm\Realm\Realm.cs:136 
  at Realms.Realm.GetInstance (Realms.RealmConfigurationBase config) [0x00000] in C:\jenkins\workspace\realm_realm-dotnet_PR\Realm\Realm\Realm.cs:81 
  at UnitTestREALM.TransactionsRealmApiTest.RealmApi () [0x00001] in /Users/user/Projects/myApp/UnitTestREALM/TransactionsRealmApiTest.cs:26 
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj,System.Reflection.BindingFlags invokeAttr,System.Reflection.Binder binder,System.Object[] parameters,System.Globalization.CultureInfo culture) [0x0006a] in /Users/builder/jenkins/workspace/build-package-osx-mono/2020-02/external/bockbuild/builds/mono-x64/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:395 
--DllNotFoundException
  at (wrapper managed-to-native) Realms.SynchronizationContextEventLoopSignal.install_eventloop_callbacks(Realms.SynchronizationContextEventLoopSignal/get_eventloop,Realms.SynchronizationContextEventLoopSignal/post_on_event_loop,Realms.SynchronizationContextEventLoopSignal/release_eventloop)
  at Realms.SynchronizationContextEventLoopSignal.Install () [0x0003b] in C:\jenkins\workspace\realm_realm-dotnet_PR\Realm\Realm\Native\SynchronizationContextEventLoopSignal.cs:122 
  at Realms.NativeCommon.Initialize () [0x00112] in C:\jenkins\workspace\realm_realm-dotnet_PR\Realm\Realm\Native\NativeCommon.cs:91 
  at Realms.SharedRealmHandle..cctor () [0x00000] in C:\jenkins\workspace\realm_realm-dotnet_PR\Realm\Realm\Handles\SharedRealmHandle.cs:128

因此,我无法创建Realm实例的要点。
要与Realm配合使用,必须启动iPhone仿真器吗? 是否有可能为Realm创建单元测试? 还是应该进行某种自动测试?

解决方法

我发现,解决方案是iOS单元测试应用程序(解决方案->添加->新项目-> iOS->测试->单元测试应用程序)。 缺点很少,但是可以测试Realm。下一个挑战是运行它并通过TeamCity获得结果。