这是我的测试课
[TestClass]
public class FooServiceTest
{
private IYourRepository _yourRepository;
[TestInitialize]
public void Initialize()
{
_yourRepository = new Mock<YourRepository>().Object;
}
[TestMethod]
public void GetPushableEntries_gets_all_pushable_entries()
{
var yourObjectList = new List<YourObject>
{
new WaitingQueue
{
ProfileId = 26,
IsDeleted = false,
Pushable = true
},
new WaitingQueue
{
ProfileId = 27,
IsDeleted = false,
Pushable = true
},
new WaitingQueue
{
ProfileId = 28,
IsDeleted = false,
Pushable = false
}
};
foreach (var yourObject in yourObjectList)
{
_yourRepository.Create(yourObject);
}
var pushableEntries = _yourRepository.GetList(x => x.Pushable);
pushableEntries.Count.ShouldEqual(2);
pushableEntries.ShouldNotBeNull();
pushableEntries.ShouldBe<IReadOnlyCollection<WaitingQueue>>();
}
}
这是ShouldEqual方法
public static T ShouldEqual<T>(this T actual, object expected)
{
Assert.AreEqual(expected, actual);
return actual;
}
这是GetList方法
public IReadOnlyCollection<T> GetList(Expression<Func<T, bool>> @where, params Expression<Func<T, object>>[] nav)
{
using (var dbContext = new MyDbContext())
{
return GetFiltered(dbContext, nav).Where(where).ToList();
}
}
每次我运行GetPushableQueues_gets_all_pushable_entries()方法
实际值增加2。
Assert.AreEqual failed. Expected:<2>. Actual:<2>. //first run
Assert.AreEqual failed. Expected:<2>. Actual:<4>. //second run
Assert.AreEqual failed. Expected:<2>. Actual:<6>. //third run
即使我清理测试项目并重建它,这个问题仍然存在。知道为什么会发生这种情况以及我错过了什么吗?
注意:还有其他测试方法使用_yourRepository和调用Create方法来创建实体。
HUWWW
相关分类