I have a custom DisconnectedDbContext for use with self state tracking POCOs in a web app.
public abstract class DisconnectedDbContext : DbContext
{
protected DisconnectedDbContext()
{
var objAdapterContext = ((IObjectContextAdapter)this).ObjectContext;
}
}
I subclass this for some unit testing:
public class FruityContext : DisconnectedDbContext
{
public virtual DbSet<FruitBowl> FruitBowls { get; set; }
public virtual DbSet<Fruit> Fruits { get; set; }
}
And using Moq in a TestMethod as below:
[TestMethod]
public void CreateAFruityContext()
{
var dbc = new FruityContext();
Assert.IsNotNull(dbc);
var mockSet = new Mock<DbSet<FruitBowl>>();
var mockContext = new Mock<FruityContext>();
mockContext.Setup(m => m.FruitBowls).Returns(mockSet.Object);
var mo = mockContext.Object;
Assert.IsNotNull(mo);
}
Now this is not an actual TestMethod so I don't want to get sidetracked about that.
My problem is that for the creation of var dbc in this method, objAdapterContext in the constructor call is not null however for var mo it is null. I need objAdapterContext to be not null, as per non-mocked objects because I tap into this to handle the ObjectMaterialized event of the ObjectContext.
So the Moq wrapper is changing the behaviour of my code. Is there something I can do about this?
Aucun commentaire:
Enregistrer un commentaire