Should this case of Assert.AreSame return true?

I am testing out a repository template I created and I am using the Moq package to mock my objects. I wanted to check references to 2 objects, but the result amazes me. Here's a test:

Mock<Repository<Web_Documents>> moqRepo;
Mock<Repository<Web_Documents>> moqRepo2;

public void ObjEqGetTest()
{
    //context is DBContext and has been initialized using [TestInitialize] annotation
    moqRepo = new Mock<Repository<Web_Documents>>(context);
    moqRepo2 = new Mock<Repository<Web_Documents>>(context);
    var test = moqRepo.Object.Get(1L);
    var test2 = moqRepo2.Object.Get(1L);
    Assert.AreSame(test, test2);
}

      

And my Get method returns:

return entities.SingleOrDefault(predicate)

      

predicate

is generated with Expression

builder (I can add code if needed).

Why Assert

does this one return true when I created two different objects?

Is it that the Get method returns the same reference whenever you fetch data from the DB (since it points to the model being used)?

Thank you for your help!

EDIT @CodeCaster said that Mocking repos will return null in the request I made. But when I check the values ​​in my Web_Documents table, Assertions returns true. Let me demonstrate this:

public void IdExistsGetTest()
{
    moqDoc = new Mock<Repository<Web_Documents>>(context);
    var testDoc = moqDoc.Object.Get(1L);
    Assert.AreEqual(testDoc.NomDocument, "Ajouter une catégorie");
}

      

This test succeeds, and in Web_Documents the line in ID = 1

has NomDocument = "Ajouter une catégorie"

.

+3


source to share


2 answers


I am assuming context

this is the Entity Framework context. You are sharing it between your two repositories, so your two requests in different repositories will return the same object, because the Entity Framework "caches" objects (in a sense) within the context. When it sees that your query is returning an object that is already bound to the context (in this case, returned by the first query), it will return the same object again for the second (the objects are "the same" if they have the same primary key and type ).



+4


source


Evk has already answered why the return values ​​are identical: DbContext returns the same instance for the same primary key.

I wanted to say something about your code. Here:

moqRepo = new Mock<Repository<Web_Documents>>(context);
moqRepo2 = new Mock<Repository<Web_Documents>>(context);

      

You are mocking the class you want to test. You shouldn't mock the class under test, you should mock its dependencies.



So mock the DbContext and inject this into your unmocked repositories:

var contextMock = new Mock<DbContext>(MockBehavior.Strict);

var repo = new Repository<Web_Documents>(MockBehavior.Strict, contextMock.Object);
var repo2 = new Repository<Web_Documents>(MockBehavior.Strict, contextMock.Object);

      

Now you need to tweak contextMock

to get back what you want. when it is called in a way that it is called with methods Get()

.

See Mocking EF DbContext with Moq for more mocking of Entity Framework.

+2


source







All Articles