I am testing a repository pattern I have created and I use Moq package to mock my objects. I wanted to test references from 2 objects, but the result kind of surprises me. Here is the 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
being created using Expression
builder (I can add code if needed).
Why does this Assert
return true, when I have created two differents objects?
Is it that the Get method returns the same reference whenever you fetch data from a DB (since it points to the model being used)?
Thanks for your help!
EDIT @CodeCaster said Mocking repos will return null in the request I made. But When I verify values in my table Web_Documents, Assertions return 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 is successful, and in Web_Documents, the row at ID = 1
has NomDocument = "Ajouter une catégorie"
.