[NSub說明書] Page 8 - InOrder

NSubstitute -  InOrder
範例程式碼放在 GitHub

前言

程式執行順序也是可以驗證的

驗證執行順序

直接看程式碼

        [TestMethod]
        public void NSubstituteNote20()
        {
            //arrange
            var service = Substitute.For<IService>();
            var total = Substitute.For<ITotal>();
            var send = Substitute.For<ISend>();
            var sut = new MyDemo(service, total, send);
            //act
            sut.Do();
            //assert
            Received.InOrder(() =>
            {
                service.GetCount();
                total.GetTotal();
                send.Send();
            });

            //若順序不對 會是錯誤訊息是
            /*
            Expected to receive these calls in order:

                IService.GetCount()
                ITotal.GetTotal()
                ISend.Send()

            Actually received matching calls in this order:

                ISend.Send()
                IService.GetCount()
                ITotal.GetTotal()
            */
        }

重點是Received.InOrder

裡面就是驗證執行順序

結語

可以驗證順序實在很方便阿

如果內容有誤請多鞭策謝謝