How to add an item to a Mock DbSet (using Moq)

后端 未结 2 986
悲&欢浪女
悲&欢浪女 2020-12-23 12:25

I\'m trying to set up a mock DbSet for testing purposes. I used the tutorial here, http://www.loganfranken.com/blog/517/mocking-dbset-queries-in-ef6/ and slightly modified i

2条回答
  •  一个人的身影
    2020-12-23 13:08

    To handle .Find(), we can use reflection in a similar manner, with a few assumptions about conventions when utilizing Find.

    (hopefully this isn't out of line, I've had this Q/A bookmarked for years, and was looking for a Find implementation...)

    Implement another helper as so:

    static object find(IEnumerable oEnumerable, object[] keys)        
    {
        // assumptions: primary key of object is named ID
        // primary key of object is an int
        // keys passed to .Find() method is a single value of int type
        foreach (var o in oEnumerable)
        {
            var t = o.GetType();
            var prop = t.GetProperty("ID");
            if (prop != null)
            {
                if (prop.PropertyType == typeof(int))
                {
                    if ((int)prop.GetValue(o) == (int)keys[0])
                    {
                        return o;
                    }
                }
            }                
        }
        return null;
    }
    
    

    and in the mock setup example provided by Daniel above:

    dbSet.Setup(d => d.Find(It.IsAny())).Returns((object[] oArray) => find(sourceList, oArray) as T);
    

    Beacuse we have no way (or desire, usually) to determine the primary key in the way that EF would, I'm making the assumption that "ID" is the key field name (which matches my own conventions), but this could be expanded upon to accept a number of variations. I'm also assuming that only one integer is passed to Find (as is my standard convention), but this could also be expanded on for more robust support.

    提交回复
    热议问题