Moq EF 6 DBSet, Setup FirstOrDefault method

I would like testing private method. When I setup FirstOrDefault method Moq thrown Exception. In my algorithm, I want check that, does my method properly create new objects. See my code below.

var data = new List<TypeValue>();

var typeValueMockSet = new Mock<DbSet<TypeValue>>();

    .Setup(m => m.GetEnumerator())

//throw the Error => Expression references a method that does not belong to the mocked object:
// m => m.FirstOrDefault<TypeValue>(It.IsAny<Expression`1>())
    .Setup(m => m.FirstOrDefault(It.IsAny<Expression<Func<TypeValue, bool>>>()))

    .Setup(m => m.Add(It.IsAny<TypeValue>()))
    .Returns((TypeValue vt) => vt)
    .Callback((TypeValue vt) => data.Add(vt));

var mockContext = new Mock<EngineeringWorkEntities>();
mockContext.Setup(m => m.TypeValues).Returns(typeValueMockSet.Object);

int counter = 0;
    .Setup(m => m.SaveChanges())
    .Callback(() => data
        (item) => 
            item.Id = counter;

IMakro makroDateGenerate = new MakroDataGenerate(mockContext.Object);

var pObj = new PrivateObject(makroDateGenerate);

int r1 = (int)pObj.Invoke("GetTypeValueId", "0,2% k/k");        //2 


You cannot mock FirstOrDefault because it is an extension method. It doesn't belongs to List<T> or anything else.

You cannot test private methods other than by using Reflection in the unit tests. This is very cumbersome. You should test the public interface anyway.

Either make the method public or internal. If you make it internal, you can allow the unit test project to see it by the use of the InternalsVisibleToAttribute.


Need Your Help

Android to DjangoServer - Problems with Unique Identifiers for Users - hard code works, sharedprefs doesnt

android python django string sharedpreferences

Okay, here is my issue. On first install on the Android I create a uniqueID for the phone(they look like this: 6458e138-76c3-8jf3-9708-69d701081cf1). I then POST that id to the server and the ser...

What's the best way to save a checkboxlist so the user can open the list after re-log

php mysql storage checkboxlist

I'm able to get all the checked values but I have to reconstruct the whole Checklist again.