コード例 #1
0
        public void LoadWithCallback_TestMethod()
        {
            var cacheDataController = new CacheDataController();
            var mockCacheData       = new Mock <ICacheData>();

            mockCacheData.SetupProperty(m => m.IsCached, false);
            mockCacheData.Setup(m => m.Load(false)).Callback(() => mockCacheData.Object.IsCached = true);
            var callbackExecuted = false;

            cacheDataController.Initialize(mockCacheData.Object);
            cacheDataController.LoadWithCallback(() => { callbackExecuted = true; });

            // Wait for a maximum of 1 second for the callback to be called
            for (int i = 0; i < 10; i++)
            {
                if (callbackExecuted)
                {
                    break;
                }
                Thread.Sleep(100);
            }

            if (!callbackExecuted)
            {
                Assert.Fail("Boolean value was not set to true");
            }

            mockCacheData.Verify(m => m.Load(false), Times.Once());
            Assert.IsTrue(cacheDataController.IsCached, "IsCached should be true after load");
        }
コード例 #2
0
        public void Initialize_TestMethod()
        {
            var cacheDataController = new CacheDataController();
            var mockCacheData       = new Mock <ICacheData>();

            cacheDataController.Initialize(mockCacheData.Object);

            Assert.AreSame(mockCacheData.Object, cacheDataController.Data, "CacheData should be set");
        }
コード例 #3
0
        public void LoadTwice_TestMethod()
        {
            var cacheDataController = new CacheDataController();
            var mockCacheData       = new Mock <ICacheData>();

            mockCacheData.SetupProperty(m => m.IsCached, false);
            mockCacheData.Setup(m => m.Load(false)).Callback(() => mockCacheData.Object.IsCached = true);

            cacheDataController.Initialize(mockCacheData.Object);
            cacheDataController.Load();
            cacheDataController.Load();

            mockCacheData.Verify(m => m.Load(false), Times.Once());
        }