我有一個從表返回的記錄列表中的泛型方法:單元測試泛型方法與最小起訂量
public List<T> GetValidRecords<T>() where T: class, IGetListOfTables
{
try
{
return _context.Set<T>().Where(x => x.Valid == 1).ToList();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
,我有此方法的單元測試:
[TestMethod]
public void GetValidRecords()
{
var data = new List<tableName>
{
new tableName() {Valid= 1},
new tableName() {Valid= 1}
}.AsQueryable();
var mockSet = new Mock<DbSet<tableName>>();
mockSet.As<IQueryable<tableName>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<tableName>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<tableName>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<tableName>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
var mockContext = new Mock<ALMONEntitiesNew>();
mockContext.Setup(x => x.tableName).Returns(mockSet.Object);
var database = new Database(mockContext.Object);
var numberOfRecords = database.GetValidRecords<tableName>();
Assert.AreEqual(2, numberOfRecords.Count, "Wrong number of valid records.");
}
的問題是,我從表中獲得實際的記錄數,而不是數量。 我該如何解決它?
你沒有'Setup'爲'設置<>'方法 –
@JordyLangen你能提供樣品嗎? – Sasha