Different return values the first and second time with Moq

后端 未结 7 632
感动是毒
感动是毒 2021-01-29 18:51

I have a test like this:

    [TestCase("~/page/myaction")]
    public void Page_With_Custom_Action(string pat         


        
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-29 19:10

    The existing answers are great, but I thought I'd throw in my alternative which just uses System.Collections.Generic.Queue and doesn't require any special knowledge of the mocking framework - since I didn't have any when I wrote it! :)

    var pageModel = new Mock();
    IPageModel pageModelNull = null;
    var pageModels = new Queue();
    pageModels.Enqueue(pageModelNull);
    pageModels.Enqueue(pageModel.Object);
    

    Then...

    repository.Setup(x => x.GetPageByUrl(path)).Returns(pageModels.Dequeue);
    

提交回复
热议问题