0

I want to create a mocked DbContext using faked DbSets. So I've created a class called FakeDbSet<T> which implements IDbSet<T>.

Now I've created a FakeDbContext which has those faked DbSet<T>s in it. I've bound DbContext to FakeDbContext with Ninject like this:

Kernel.Bind<DbContext>().To<FakeDbContext>();

When I call my WebAPI project I'll get an SqlException that I don't have the permission to create the database.

This is my FakeDbContext:

public class FakeDbContext : DbContext
{
    public virtual IDbSet<User> Users => new UserDbSet();
}

(Note: UserDbSet just fills the HashSet<User> with default data.)

How do I set my FakeDbContext to not create/interact with a real database? If I remove the : DbContext from the FakeDbContext Ninject says that it must be convertible to DbContext.

1

2 Answers 2

2

Try disabling database initialisation

public class FakeDbContext : DbContext
{
    static FakeDbContext()
    {
        Database.SetInitializer<FakeDbContext>(null);
    }

    public FakeDbContext() { }

    public virtual IDbSet<User> Users => new UserDbSet();
}

This test works for me:

[Fact]
public void Test1()
{
    var kernel = new StandardKernel();
    kernel.Bind<RealContext>().To<FakeDbContext>();

    var result = kernel.Get<Wrapper>();

    var user = result.context.Users.Add(new User());
    Assert.NotNull(user);
}

public class Wrapper
{
    public readonly RealContext context;

    public Wrapper(RealContext context)
    {
        this.context = context;
    }
}

public class RealContext : DbContext
{
    public virtual IDbSet<User> Users => new UserDbSet();
}

public class FakeDbContext : RealContext
{
    static FakeDbContext()
    {
        Database.SetInitializer<FakeDbContext>(null);
    }

    public override IDbSet<User> Users => new UserDbSet();
}

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

8 Comments

Thanks this worked for me, but now I'm getting an error saying The underlying provider failed on Open.. How do I disable all interaction with real tables and databases?
Does an empty constructor help?
Unfortunately not. It's stil trying to connect to the database on System.Data.Entity.Core.EntityClient.EntityConnection.Open().
It's still trying to connect to that database. (I left the connectionString empty) Our problem is that we don't have a DB yet so we have to work with mocked data. How do I fix this?
It's done by Ninject, I'm just injecting a DbContext into my services. In my question there's the line with the binding.
|
0

I've finally fixed ith with qujck's help.

The reason why I was getting an exception because my DbContext tried to connect to the database was, that I was using DbContext.Set<T> which was accessing the database.

Overriding the Set<T> method fixed the problem.

Comments

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.