6

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.

9
  • 3
    @Aron actually, it can work with or without a backing file. Using it as memory-only is a very common scenario. That's what this question is about. Commented May 24, 2016 at 9:04
  • 1
    This is a perfectly valid question. Any downvoters should give a valid explanation Commented May 24, 2016 at 9:05
  • @Aron sqlite.org/inmemorydb.html Commented May 24, 2016 at 9:07
  • @Aron then you are looking at the wrong page. Using SQLite as a memory-only database is a common scenario Commented May 24, 2016 at 9:07
  • @stuartd I stand corrected. Commented May 24, 2016 at 9:07

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

that works. But do you know how to use more than one in-memory database so that tests in paralell can run without stepping on each other's toes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.