Stam
Stam

Reputation: 2490

Is it possible to create an In-memory database using SQLite.Net-PCL?

Currently I am working on unit tests for a Xamarin MvvmCross Application using SQLite.Net-PCL 3.1.1. The unit test project is a normal .NET project.

So now I am mocking the MvxSqliteConnectionFactoryBase

public class MockMvxSqliteConnectionFactoryBase :  MvxSqliteConnectionFactoryBase
{
    #region implemented abstract members of MvxSqliteConnectionFactoryBase

    public override string GetPlattformDatabasePath(string databaseName)
    {
        return "Data Source=:memory:";
    }

    public override ISQLitePlatform GetCurrentPlatform()
    {
        return new SQLite.Net.Platform.Generic.SQLitePlatformGeneric(); 
    }
    public override ISQLitePlatform GetCurrentPlatform(string key)
    {
        return new  SQLite.Net.Platform.Generic.SQLitePlatformGeneric();
    }

    #endregion
}

But the database is not created in memory rather than in the bin folder of the project.

I tried to initialize SQLiteConnection like this example but there is not a constructor that accepts only one string.

Upvotes: 6

Views: 2433

Answers (1)

Stam
Stam

Reputation: 2490

Following the hint that @CL. gave me I changed the method GetPlattformDatabasePath:

public override string GetPlattformDatabasePath(string databaseName)
    {
        return ":memory:";
    }

Now I don't see something to be created in the bin folder and there is no need for the extisting data to be deleted after every unit test run, so the database must be in-memory.

Upvotes: 4

Related Questions