Hi All,
Can someone please tell me why attempting to mock entityFramwork throws a compile time error on this line?
Isolate.WhenCalled(() => contextHandle.LPRProvinceCodes.Find(0)).WillReturnCollectionValuesOf(pcodes.AsQueryable());
The error is: *** Method System.Data.Entity.DbSet`1[PlateTech.Shared.Models.LPRProvinceCode].Find returns PlateTech.Shared.Models.LPRProvinceCode. Can only use WillReturnCollectionValuesOf() on enumerable types.
Here is my test code:
[TestMethod, Isolated]
public void CheckpointTest2()
{
LPRInterfaceMessage lprMsg = new LPRInterfaceMessage()
{
... msg properties go here
};
Object[] lprsMessages = new Object[] { lprMsg };
// Mock the EF context
var contextHandle = Isolate.Fake.AllInstances<EFContext>(Members.CallOriginal);
IList<LPRProvinceCode> pcodes = new List<LPRProvinceCode>()
{
new LPRProvinceCode()
{
LPRCountryCodeId = 999,
ProvinceCode = \\\"999\\\",
ProvinceName = \\\"Unknown\\\"
}
};
Isolate.WhenCalled(() => contextHandle.LPRProvinceCodes.Find(0)).WillReturnCollectionValuesOf(pcodes.AsQueryable());
LPRInterface lpi = new LPRInterface();
lpi.ThreadedAddLPREvent(lprsMessages);
}
}
here is the EF call that I am trying to mock:
public string FindProvinceByCode(string provinceCode)
{
if (int.TryParse(provinceCode, out int provinceCodeValue))
{
var results = _context.LPRProvinceCodes.Find(provinceCodeValue);
if (results != null)
{
return results.ProvinceName;
}
}
return string.Empty;
}