Esempio n. 1
0
        public void StartExport()
        {
            // arrange
            var exportUtility = new ExportUtility();

            var exportViewModel = new ExportOptionsViewModel();
            exportUtility.TheExportOptionsViewModel = new Lazy<ExportOptionsViewModel>(() => exportViewModel);

            var shell = Mock.Create<IShell>(Behavior.Loose);
            exportUtility.TheWindowManager = new Lazy<IShell>(() => shell);

            var dynamicTypeManager = Mock.Create<IDynamicTypeManager>(Behavior.Loose);
            exportUtility.TheDynamicTypeManager = new Lazy<IDynamicTypeManager>(() => dynamicTypeManager);

            Mock.Arrange(() => Utils.CurrentUserAccountId).Returns(-1);

            var beginInsertEmailLogWasCalled = false;
            Mock.Arrange(() => MethodCaller.CallFactoryMethod(Arg.IsAny<Type>(), Arg.AnyString, Arg.IsAny<object[]>())).DoInstead(() => beginInsertEmailLogWasCalled = true);

            var popupFactory = new PopupFactory();

            var notifySuccessCalled = false;
            Mock.Arrange(() => popupFactory.NotifySuccess(LanguageService.Translate("Msg_ProcessExported"), "Popup_Success")).DoInstead(() => notifySuccessCalled = true);

            var notifyFailureCalled = false;
            Mock.Arrange(() => popupFactory.NotifyFailure(LanguageService.Translate("Msg_ExportError"), "Popup_Error", 3, false)).DoInstead(() => notifyFailureCalled = true);

            exportUtility.ThePopupFactory = new Lazy<PopupFactory>(() => popupFactory);

            var exportMethodWasCalled = false;
            Mock.NonPublic.Arrange<bool>(exportUtility, "Export", new object[] { ArgExpr.IsAny<GridViewDataControl>(), ArgExpr.IsAny<string>() }).DoInstead(() => exportMethodWasCalled = true).Returns(true);

            // act
            exportUtility.ExportStart("eCar_p", new GridViewDataControl(), "Id|CurrentState");

            // assert
            Assert.IsTrue(beginInsertEmailLogWasCalled);
            Assert.IsTrue(notifySuccessCalled);
            Assert.IsFalse(notifyFailureCalled);
            Assert.IsTrue(exportMethodWasCalled); // After exporting, we have to return it as it was, that is, back 25 info items

            // arrange
            notifySuccessCalled = notifyFailureCalled = false;
            Mock.NonPublic.Arrange<bool>(exportUtility, "Export", new object[] { ArgExpr.IsAny<GridViewDataControl>(), ArgExpr.IsAny<string>() }).Returns(false);

            // act
            exportUtility.ExportStart("eCar_p", new GridViewDataControl(), "Id|CurrentState");

            // assert
            Assert.IsFalse(notifySuccessCalled);
            Assert.IsTrue(notifyFailureCalled);
        }
        public void DownloadImageCommand()
        {
            // arrange
            var vm = new ImageFieldViewModel();

            var saveFileDialog = Mock.Create<SaveFileDialog>(Behavior.CallOriginal);
            Mock.Arrange(() => saveFileDialog.ShowDialog()).Returns(false);

            var privateAccessor = new PrivateAccessor(vm);
            privateAccessor.SetField("_saveFileDialog", saveFileDialog);

            Mock.NonPublic.Arrange<bool>(vm, "IsImageExists", new object[] { ArgExpr.IsAny<object>() }).Returns(true);

            var editableRoot = Mock.Create<IEditableRoot>(Behavior.Loose);
            Mock.Arrange(() => editableRoot.ProcessName).Returns("eCar_p");
            Mock.Arrange(() => editableRoot.Id).Returns(1);

            Mock.Arrange(() => vm.ParentModel).Returns(editableRoot);

            Mock.Arrange(() => vm.Property).Returns(GetType().GetProperty("Image2"));

            // act
            try
            {
                vm.DownloadImageCommand.Execute(null);
            }
            catch(Exception ex)
            {
                // assert
                Assert.Fail("Expected no exception, but got: " + ex.Message);
            }

            // arrange
            Mock.Arrange(() => saveFileDialog.ShowDialog()).Returns(true);

            Mock.Arrange(() => saveFileDialog.OpenFile()).Returns(new MemoryStream());

            var array = new byte[8 * 1024 + 1];
            for (var i = 0; i < array.Length; i++)
            {
                array[i] = new byte();
            }

            vm.Value = array;

            var popupFactory = new PopupFactory();
            var notifySuccessCalled = false;
            Mock.Arrange(() => popupFactory.NotifySuccess(LanguageService.Translate("Msg_SuccessfullySaved"), "Popup_Success")).DoInstead(() => notifySuccessCalled = true);

            vm.ThePopupFactory = popupFactory;

            // act
            vm.DownloadImageCommand.Execute(null);

            // assert
            Assert.IsTrue(notifySuccessCalled);
            Assert.IsNull(privateAccessor.GetField("_saveFileDialog"));
        }