亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

MsTest :每次測試方法運行時實際值都會增加

MsTest :每次測試方法運行時實際值都會增加

C#
慕蓋茨4494581 2021-11-28 19:40:40
這是我的測試課[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 runAssert.AreEqual failed. Expected:<2>. Actual:<4>. //second runAssert.AreEqual failed. Expected:<2>. Actual:<6>. //third run即使我清理測試項目并重建它,這個問題仍然存在。知道為什么會發生這種情況以及我錯過了什么嗎?注意:還有其他測試方法使用_yourRepository和調用Create方法來創建實體。
查看完整描述

1 回答

?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

問題是您實際上在那里使用某種存儲庫。你不要嘲笑它。_yourRepository = new Mock<YourRepository>().Object;

應該是 _yourRepository = new Mock< IYourRepository >().Object;

并且您從 IYourRepository 接口使用的所有方法也應該被模擬/設置。


查看完整回答
反對 回復 2021-11-28
  • 1 回答
  • 0 關注
  • 193 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號