Different return values the first and second time with Moq

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

I have a test like this:

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


        
7条回答
  •  时光取名叫无心
    2021-01-29 19:07

    Adding a callback did not work for me, I used this approach instead http://haacked.com/archive/2009/09/29/moq-sequences.aspx and I ended up with a test like this:

        [TestCase("~/page/myaction")]
        [TestCase("~/page/myaction/")]
        public void Page_With_Custom_Action(string virtualUrl) {
    
            // Arrange
            var pathData = new Mock();
            var pageModel = new Mock();
            var repository = new Mock();
            var mapper = new Mock();
            var container = new Mock();
    
            container.Setup(x => x.GetInstance()).Returns(repository.Object);
            repository.Setup(x => x.GetPageByUrl(virtualUrl)).ReturnsInOrder(null, pageModel.Object);
    
            pathData.Setup(x => x.Action).Returns("myaction");
            pathData.Setup(x => x.Controller).Returns("page");
    
            var resolver = new DashboardPathResolver(pathData.Object, repository.Object, mapper.Object, container.Object);
    
            // Act
            var data = resolver.ResolvePath(virtualUrl);
    
            // Assert
            Assert.NotNull(data);
            Assert.AreEqual("myaction", data.Action);
            Assert.AreEqual("page", data.Controller);
        }
    

提交回复
热议问题