how to unit test DB operations (xamarin cross platform)dependency service in portable class library.

jakjak INMember

how to write a unit test for sqlite database operations in xamarin cross platform (portable)?

I am using Dependency service to fetch the connection from platform specific, and when i try to write a unit test it is throwing "You MUST call Xamarin.Forms.Init(); prior to using it." exception which is obvious.

Any tutorial or sample code for unit testing xamarin cross platform (portable) app using dependency services would be very helpful...

public class ProjectManager
{
static object locker = new object();
SQLiteConnection database;

    public ProjectManager()
    {

        database = DependencyService.Get<IDataBase>().getconnection();//exception here   (You MUST call Xamarin.Forms.Init(); prior to using it.)


    }
    public int SaveProject(Project item)
    {
        lock (locker)
        {
            int projectID = getProjectByID(item.id);
            if (projectID != 0)
            {
                item.RID = projectID;
                database.Update(item);
                return item.RID;
            }
            else
            {
                return database.Insert(item);
            }
        }
    }
    public List<Project> GetProjectDetails()
    {
        lock (locker)
        {
            return (from i in database.Table<Project>() select i).ToList();
        }
    }


    public int getProjectByID(string id)
    {
        lock (locker)
        {
            Project project = database.Table<Project>().FirstOrDefault(x => x.id == id);
            if (project != null)
            {
                return project.RID;
            }
            else
            {
                return 0;
            }
        }
    }
}

Answers

Sign In or Register to comment.