Using Realm in xUnit tests


#1

I am testing our Realm data layer in a .NET Core 2.2 Test project (xUnit). When realm.WriteAsync is called, it never returns. Does Realm work with unit testing? Surely it does, but I am not sure why this simple example fails. I am just using a default Realm configuration btw.

Would an in memory Realm fix this issue? I have seen posts talking about in memory Realms, but none of the code to create these seem to be in my NuGet package. I am not seeing anything about that in the Realms docs either.

Also, if I use the realm.Write vs. realm.WriteAsync, everything works fine.

        [Fact]
        public async Task realm_db_test()
        {
            var realm = Realm.GetInstance();

            // clear all locations
            realm.Write(() =>
            {
                realm.RemoveAll<Location>();
            });

            // make sure location table is empty
            Assert.True(realm.All<Location>().Count() == 0);

            // THE CODE NEVER RETURNS FROM THIS CALL, NO EXCEPTION IS THROWN
            await realm.WriteAsync((r) =>
            {
                r.Add(new Location()
                {
                    Latitude = .1,
                    Longitude = .2
                });
            });
        }


#2

Does using a non-async write work for you?


#3

Yes, non async works fine.


#4

This is the implementation of WriteAsync - I would try to paste it in your project and see where things go wrong. Unfortunately, we’re fairly busy with other tasks, so it’s unlikely that anyone from Realm will be able to debug that in the next few weeks. I’d be happy to help/give pointers though.


#5

I cloned the Realm repo and added the Realm.csproj directly into my solution. I then removed the Realm NuGet packages and referenced Realm directly from my test project. I upgraded the .NET Standard setting to 2.0. Everything compiles.

When I run a test I am getting an error “No RealmObjects. Has linker stripped them?” on the Realm.GetInstance() call.

Can you provide some guidance on what is required to successfully get Realm to run as a standalone project in my solution and directly referenced by the other projects?

Fyi, I am targeting .NET Standard 2.0. I noticed Realm was targeting the old 1.6, so that’s why I upgraded it per above…

Thanks!


#6

You need to add the build targets (you can find them in the nuget folder). You also need to add reference to the native binaries - I would recommend using the built ones that come from nuget as opposed to trying to build the native bits yourself. As for the target framework - Realm is targeting the lowest possible version to make sure it covers the broadest range of projects (notably UWP).


#7

There seems to be something with Realm and xUnit where they don’t get along. I found this article on github from a couple of year’s ago with someone having a similar issue. I just switched my test project over to NUnit and it works fine. And NUnit suits our needs just fine, so we will go with that.