public void TestOnStepAsyncAtCancel() { GeneralThreadAffineContext.Run( async () => { Mock.Arrange(() => Application.Current.Dispatcher.InvokeAsync(null)) .IgnoreArguments() .DoNothing(); var managementMock = Mock.Create<CollectionsManagementViewModel>(Behavior.CallOriginal); var editQueueMock = Mock.Create<EditQueueViewModel>( Behavior.CallOriginal, managementMock); Mock.NonPublic.Arrange<Task>(editQueueMock, "UnLockAsync").Returns(Task.Run(() => { })); Mock.Arrange(() => editQueueMock.SelectedQueue).Returns(new QueueDetailsModel()); Mock.Arrange( () => managementMock.OnStepAsync(CollectionsManagementViewModel.EnumSteps.SelectQueue)) .Returns(Task.Run(() => { })); var mainView = new PrivateAccessor(editQueueMock); mainView.SetProperty("IsCheckedOut", true); await editQueueMock.OnStepAsync(EditQueueViewModel.EnumSteps.Cancel); bool isCheckedOutOnMainViewModel = mainView.GetProperty("IsCheckedOut") is bool && (bool)mainView.GetProperty("IsCheckedOut"); Assert.IsFalse(isCheckedOutOnMainViewModel); }); }
public void PrivateAccessor_ShouldGetSetProperty() { // ACT // Wrapping the instance holding the private property. var inst = new PrivateAccessor(new ClassWithNonPublicMembers()); // Setting the value of the private property. inst.SetProperty("Prop", 555); // ASSERT - Asserting with getting the value of the private property. Assert.AreEqual(555, inst.GetProperty("Prop")); }
public void MaxFileSize() { // arrange var vm = new ImageFieldViewModel(); // act var privateAccessor = new PrivateAccessor(vm); var maxFileSize = privateAccessor.GetProperty("MaxFileSize"); // assert Assert.AreEqual((ulong)0, maxFileSize); // arrange var systemOptions = Mock.Create<ISystemOptionsInfo>(Behavior.Loose); systemOptions.MaxFileSize = 10 * 10 * 1024; Mock.Arrange(() => SystemOptionsInfo.SystemOptions).Returns(systemOptions); // act maxFileSize = privateAccessor.GetProperty("MaxFileSize"); // assert Assert.AreEqual((ulong)10 * 10 * 1024, maxFileSize); }
public void IsSelectedPropertyTests() { var tvrs = new TreeViewRequiredStepEdit { DisplayFields = new TreeViewDisplayFields() }; var tree = new TreeViewRequiredStepViewModel(tvrs, new StepInfo(), new ProcessFieldViewModel(new FieldEdit(), new ProcessSectionViewModel(new SectionEdit()))); // Arrange var mockedClass = Mock.Create<TreeViewDisplayFieldViewModel>(Behavior.Loose); Mock.NonPublic.Arrange<ITreeViewRequiredStepViewModel>(mockedClass, "ParentStepViewModel").Returns(tree); // Act var inst = new PrivateAccessor(mockedClass); inst.SetProperty("IsSelected", true); var vm = inst.GetProperty("ParentStepViewModel"); // Assert Assert.IsNotNull(vm); }
public void InvokeConstructorTest() { var spreadsheet = Mock.Create<SpreadsheetView>(Behavior.CallOriginal); var privateAccessor = new PrivateAccessor(spreadsheet); //Check properties Assert.AreEqual(privateAccessor.GetProperty("DefaultStyleKey"), typeof(SpreadsheetView)); Assert.IsNotNull(spreadsheet.Columns); Assert.IsTrue(spreadsheet.AllowDrop); Assert.IsNotNull(privateAccessor.GetField("_childSpreadsheets")); var expSpreadsheetView_LayoutUpdated = Mock.NonPublic.Arrange(spreadsheet, "SpreadsheetView_LayoutUpdated", ArgExpr.IsAny<object>(), ArgExpr.IsAny<EventArgs>()); var expAutoSize = Mock.NonPublic.Arrange(spreadsheet, "AutoSize"); //Check if AutoSize is called on LayoutUpdated Mock.Raise(() => spreadsheet.LayoutUpdated += null, EventArgs.Empty); expSpreadsheetView_LayoutUpdated.MustBeCalled(); expAutoSize.MustBeCalled(); //Check if _loaded is set to true on Loaded Assert.IsFalse((bool)privateAccessor.GetField("_loaded")); Mock.Raise(() => spreadsheet.Loaded += null, new RoutedEventArgs()); Assert.IsTrue((bool)privateAccessor.GetField("_loaded")); }
public void CurrentFileProcess() { // arrange var vm = new FieldFileViewModel(); var privateAccessor = new PrivateAccessor(vm); // act var currentFileProcess = privateAccessor.GetProperty("CurrentFileProcess"); // assert Assert.IsNull(currentFileProcess); // arrange var fileProcess = Mock.Create<IFileProcess>(Behavior.CallOriginal); vm.Value = fileProcess; // act currentFileProcess = privateAccessor.GetProperty("CurrentFileProcess"); // assert Assert.AreEqual(fileProcess, currentFileProcess); }
public void SetStateUseReport() { // arrange var vm = new FieldFileViewModel(); var fileProcess = Mock.Create<IFileProcess>(x => x.Implements<ITrackStatus>()); fileProcess.UseReport = true; Mock.NonPublic.Arrange<IFileProcess>(vm, "CurrentFileProcess").Returns(fileProcess); var privateAccessor = new PrivateAccessor(vm); // act privateAccessor.CallMethod("SetState"); // assert Assert.AreEqual("Invalid setup.", privateAccessor.GetProperty("ExceptionMessage")); Assert.AreEqual(FileControlMode.Error, vm.CurrentState); // arrange fileProcess.ReportName = "eCarProcessMainReport.trdx"; // act privateAccessor.CallMethod("SetState"); // assert Assert.IsNotNull(fileProcess.FileName); Assert.AreEqual("Save item to view file.", vm.Message); Assert.AreEqual(FileControlMode.NotifySuccess, vm.CurrentState); // act privateAccessor.CallMethod("SetState"); // assert Assert.AreEqual(FileControlMode.View, vm.CurrentState); }
public void WhenFileIncludeInCheckListFileNotFound() { // arrange var vm = new FieldFileViewModel(); var fileProcess = Mock.Create<IFileProcess>(x => x.Implements<ITrackStatus>()); Mock.Arrange(() => ((ITrackStatus)fileProcess).IsChild).Returns(true); Mock.NonPublic.Arrange<IFileProcess>(vm, "CurrentFileProcess").Returns(fileProcess); var dynamicManager = Mock.Create<IDynamicTypeManager>(); var dataPortalResult = new DataPortalResult<IEditableRoot>(null, new Exception(), null); Mock.Arrange(() => dynamicManager.BeginGetEditableRoot(Constants.FileProcessName, Arg.AnyInt, Arg.IsAny<Action<object, DataPortalResult<IEditableRoot>>>())) .DoInstead<string, int, Action<object, DataPortalResult<IEditableRoot>>>((processName, id, callback) => callback(null, dataPortalResult)); vm.TheDynamicTypeManager = dynamicManager; var logger = Mock.Create<ILogger>(Behavior.CallOriginal); var wasLogged = false; Mock.Arrange(() => logger.Log(LogSeverity.Error, typeof(FieldFileViewModel).FullName, Arg.IsAny<Exception>())).DoInstead(() => wasLogged = true); vm.Logger = logger; var privateAccessor = new PrivateAccessor(vm); // act privateAccessor.CallMethod("SetState"); // assert Assert.AreEqual("File not found", vm.Message); Assert.AreEqual(new Exception().ToString(), privateAccessor.GetProperty("ExceptionMessage")); Assert.AreEqual(FileControlMode.NotifyFail, vm.CurrentState); Assert.IsTrue(wasLogged); // arrange vm.Message = null; vm.CurrentState = FileControlMode.Download; wasLogged = false; dataPortalResult = new DataPortalResult<IEditableRoot>(Mock.Create<IEditableRoot>(Behavior.Loose), null, null); // act privateAccessor.CallMethod("SetState"); // assert Assert.AreEqual("File not found", vm.Message); Assert.IsNull(privateAccessor.GetProperty("ExceptionMessage")); Assert.AreEqual(FileControlMode.NotifyFail, vm.CurrentState); Assert.IsTrue(wasLogged); }
public void GetSystemOptions() { // arrange var vm = new FieldFileViewModel(); Mock.Arrange(() => Arg.IsAny<SystemPathsInfo>().BeginGetSystemPathsAsync()).Throws<Exception>(); var logger = Mock.Create<ILogger>(Behavior.CallOriginal); var logWasCalled = false; Mock.Arrange(() => logger.Log(LogSeverity.Error, typeof(FieldFileViewModel).ToString(), Arg.IsAny<Exception>())).DoInstead(() => logWasCalled = true); vm.Logger = logger; var privateAccessor = new PrivateAccessor(vm); // act privateAccessor.CallMethod("GetSystemOptions"); // assert Assert.AreEqual("Invalid System Paths. Contact your administrator, please.", vm.Message); var exceptionMessage = (string)privateAccessor.GetProperty("ExceptionMessage"); Assert.IsTrue(exceptionMessage.StartsWith(new Exception().ToString())); Assert.AreEqual(FileControlMode.NotifyFail, vm.CurrentState); Assert.IsTrue(logWasCalled); // arrange vm.Message = null; privateAccessor.SetProperty("ExceptionMessage", new Exception().ToString()); vm.CurrentState = FileControlMode.Download; var systemPathsInfo = Mock.Create<SystemPathsInfo>(Behavior.Loose); var task = new Task<SystemPathsInfo>(() => systemPathsInfo); task.RunSynchronously(); Mock.Arrange(() => Arg.IsAny<SystemPathsInfo>().BeginGetSystemPathsAsync()).Returns(() => task); // act privateAccessor.CallMethod("GetSystemOptions"); // assert Assert.AreEqual("System Paths in System Option menu is empty. Please fill in before using FileControl", vm.Message); Assert.IsNull(privateAccessor.GetProperty("ExceptionMessage")); Assert.AreEqual(FileControlMode.NotifyFail, vm.CurrentState); // arrange vm.Message = null; privateAccessor.SetProperty("ExceptionMessage", new Exception().ToString()); vm.CurrentState = FileControlMode.Download; Mock.Arrange(() => systemPathsInfo.FileUploadURI).Returns("http://*****:*****@"D:\Projects\Cebos\Veyron\Cebos.Veyron.Web\FileStorage"); var navigateUriWasRaises = false; Mock.NonPublic.Arrange(vm, "RaisePropertyChanged", "NavigateUri").DoInstead(() => navigateUriWasRaises = true); // act privateAccessor.CallMethod("GetSystemOptions"); Assert.AreEqual("http://localhost:5558/FileUploadHandler.ashx", vm.UploadServiceUrl); Assert.AreEqual("/FileStorage", vm.TargetFolder); Assert.AreEqual("http://localhost:5558/", vm.DocHandlerUrl); Assert.IsTrue(navigateUriWasRaises); }
public void OnUploadFailed() { // arrange var vm = new FieldFileViewModel(); var fileProcess = Mock.Create<IFileProcess>(Behavior.Loose); fileProcess.IsUploading = true; fileProcess.FileName = "8AAC798B-ED88-4366-AF0A-5E727111A9CB.txt"; fileProcess.OriginalFileName = "1.txt"; Mock.NonPublic.Arrange<IFileProcess>(vm, "CurrentFileProcess").Returns(fileProcess); var e = new FileUploadFailedEventArgs(null, null, new Exception().ToString()); // act vm.OnUploadFailed(null, e); // assert Assert.IsFalse(fileProcess.IsUploading); Assert.IsNull(fileProcess.OriginalFileName); Assert.IsNull(fileProcess.FileName); var privateAccessor = new PrivateAccessor(vm); Assert.IsTrue((bool)privateAccessor.GetProperty("UploadError")); Assert.AreEqual("Invalid System Paths. Contact your administrator, please.", vm.Message); Assert.AreEqual(new Exception().ToString(), privateAccessor.GetProperty("ExceptionMessage")); Assert.AreEqual(FileControlMode.NotifyFail, vm.CurrentState); }
public void Save() { // arrange var vm = new FieldFileViewModel(); var fileProcess = Mock.Create<IFileProcess>(Behavior.Loose); fileProcess.OriginalFileName = "1.txt"; Mock.NonPublic.Arrange<IFileProcess>(vm, "CurrentFileProcess").Returns(fileProcess); var newValue = Mock.Create<IFileProcess>(Behavior.Loose); var dataPortalResult = new DataPortalResult<IFileProcess>(newValue, null, null); Mock.Arrange(() => fileProcess.BeginSaveNew(Arg.IsAny<Action<object, IDataPortalResult>>())) .DoInstead<Action<object, IDataPortalResult>>(a => a(null, dataPortalResult)); var privateAccessor = new PrivateAccessor(vm); // act privateAccessor.CallMethod("Save"); // assert Assert.AreEqual(newValue, vm.Value); Assert.AreEqual("File '1.txt' uploaded successfully.", vm.Message); Assert.AreEqual(FileControlMode.NotifySuccess, privateAccessor.GetField("_previousState")); // arrange dataPortalResult = new DataPortalResult<IFileProcess>(null, new Exception(), null); // act privateAccessor.CallMethod("Save"); // assert Assert.AreEqual("Error uploading file.", vm.Message); Assert.AreEqual(new Exception().ToString(), privateAccessor.GetProperty("ExceptionMessage")); Assert.AreEqual(FileControlMode.NotifyFail, privateAccessor.GetField("_previousState")); // arrange fileProcess.Id = 1; vm.Value = null; dataPortalResult = new DataPortalResult<IFileProcess>(newValue, null, null); Mock.Arrange(() => fileProcess.BeginUnlock(Arg.IsAny<Action<object, IDataPortalResult>>())) .DoInstead<Action<object, IDataPortalResult>>(a => a(null, dataPortalResult)); // act privateAccessor.CallMethod("Save"); // assert Assert.AreEqual(newValue, vm.Value); Assert.AreEqual("File '1.txt' uploaded successfully.", vm.Message); Assert.AreEqual(FileControlMode.NotifySuccess, vm.CurrentState); // arrange vm.Message = null; privateAccessor.SetProperty("ExceptionMessage", null); dataPortalResult = new DataPortalResult<IFileProcess>(null, new Exception(), null); var logger = Mock.Create<ILogger>(Behavior.CallOriginal); var logWasCalled = false; Mock.Arrange(() => logger.Log(LogSeverity.Error, typeof(FieldFileViewModel).ToString(), Arg.IsAny<Exception>())).DoInstead(() => logWasCalled = true); vm.Logger = logger; // act privateAccessor.CallMethod("Save"); // assert Assert.AreEqual(string.Format(@"Error saving instance of new uploaded file{0}Please try again{0}If the problem persists, try to create a new record, and re-check the System Options\System Paths", Environment.NewLine), vm.Message); Assert.AreEqual(new Exception().ToString(), privateAccessor.GetProperty("ExceptionMessage")); Assert.AreEqual(FileControlMode.NotifyFail, vm.CurrentState); Assert.IsTrue(logWasCalled); }
[Ignore] //Ignored because throws System.OutOfMemoryException public void OnUploadFinished() { // arrange var vm = new FieldFileViewModel(); var privateAccessor = new PrivateAccessor(vm); privateAccessor.SetProperty("UploadError", true); var fileProcess = Mock.Create<IFileProcess>(Behavior.Loose); fileProcess.IsUploading = true; fileProcess.OriginalFileName = "1.doc"; fileProcess.FileName = "EEE6B881-F39E-4CD0-9B0B-6CF777DCD1DA.doc"; Mock.NonPublic.Arrange<IFileProcess>(vm, "CurrentFileProcess").Returns(fileProcess); Mock.NonPublic.Arrange<string>(vm, "Message1").Throws<Exception>(); try { // act vm.OnUploadFinished(null, null); } catch (Exception ex) { // assert Assert.Fail("Expected no exception, but got: " + ex.Message); } // assert Assert.IsFalse(fileProcess.IsUploading); // arrange privateAccessor.SetProperty("UploadError", false); Mock.NonPublic.Arrange<string>(vm, "Message1").CallOriginal(); Mock.Arrange(() => fileProcess.BeginUploadFile(Arg.IsAny<Action<Exception>>())).DoInstead<Action<Exception>>(a => a(null)); Mock.NonPublic.Arrange<IFileProcess>(vm, "CurrentFileProcess").Returns(fileProcess); var saveWasCalled = false; Mock.NonPublic.Arrange(vm, "Save").DoInstead(() => saveWasCalled = true); // act vm.OnUploadFinished(null, null); // assert Assert.AreEqual("File '1.doc' uploaded successfully.", vm.Message); Assert.AreEqual(FileControlMode.NotifySuccess, vm.CurrentState); Assert.IsTrue(saveWasCalled); Assert.AreEqual("1.doc", fileProcess.OriginalFileName); Assert.AreEqual("EEE6B881-F39E-4CD0-9B0B-6CF777DCD1DA.doc", fileProcess.FileName); // arrange saveWasCalled = false; var logger = Mock.Create<ILogger>(Behavior.CallOriginal); Mock.Arrange(() => logger.Log(LogSeverity.Warning, typeof(FieldFileViewModel).ToString(), Arg.IsAny<Exception>())).DoNothing().MustBeCalled(); vm.Logger = logger; Mock.Arrange(() => fileProcess.BeginUploadFile(Arg.IsAny<Action<Exception>>())).DoInstead<Action<Exception>>(action => action(new Exception())); fileProcess.FileName = Guid.NewGuid() + ".doc"; // act vm.OnUploadFinished(null, null); // assert Assert.IsFalse(saveWasCalled); Assert.IsNull(fileProcess.OriginalFileName); Assert.IsNull(fileProcess.FileName); Assert.AreEqual("Error uploading file.", vm.Message); Assert.AreEqual(new Exception().ToString(), privateAccessor.GetProperty("ExceptionMessage")); Assert.AreEqual(FileControlMode.NotifyFail, vm.CurrentState); Mock.Assert(logger); // arrange fileProcess.OriginalFileName = "1.doc"; fileProcess.FileName = Guid.NewGuid() + ".doc"; vm.Message = null; privateAccessor.SetProperty("ExceptionMessage", null); vm.CurrentState = FileControlMode.Download; var lavel1WcfErrorInfo = new WcfErrorInfo(); var lavel2WcfErrorInfo = new WcfErrorInfo(); var lavel3WcfErrorInfo = new WcfErrorInfo(); lavel1WcfErrorInfo.InnerError = lavel2WcfErrorInfo; lavel2WcfErrorInfo.InnerError = lavel3WcfErrorInfo; lavel3WcfErrorInfo.ExceptionTypeName = typeof(DirectoryNotFoundException).FullName; lavel3WcfErrorInfo.Message = @"Directory D:\ImportantFiles\ not found"; lavel3WcfErrorInfo.StackTrace = "at ..., at ..."; var exception = new DataPortalException(lavel1WcfErrorInfo); //dataPortalResult = new DataPortalResult<FileControlUploadFileCommand>(null, exception, null); Mock.Arrange(() => fileProcess.BeginUploadFile(Arg.IsAny<Action<Exception>>())).DoInstead<Action<Exception>>(action => action(exception)); // act vm.OnUploadFinished(null, null); // assert Assert.IsFalse(saveWasCalled); Assert.IsNull(fileProcess.OriginalFileName); Assert.IsNull(fileProcess.FileName); Assert.AreEqual("Error uploading file.", vm.Message); Assert.AreEqual(string.Format(@"System.IO.DirectoryNotFoundException{0}Directory D:\ImportantFiles\ not found{0}at ..., at ...", Environment.NewLine), privateAccessor.GetProperty("ExceptionMessage")); Assert.AreEqual(FileControlMode.NotifyFail, vm.CurrentState); // arrange fileProcess.OriginalFileName = "1.doc"; fileProcess.FileName = Guid.NewGuid() + ".doc"; vm.Message = null; privateAccessor.SetProperty("ExceptionMessage", null); vm.CurrentState = FileControlMode.Download; lavel3WcfErrorInfo.ExceptionTypeName = typeof(WebException).FullName; lavel3WcfErrorInfo.Message = "Host not responding"; lavel3WcfErrorInfo.StackTrace = "at ..., at ..."; // act vm.OnUploadFinished(null, null); // assert Assert.IsFalse(saveWasCalled); Assert.IsNull(fileProcess.OriginalFileName); Assert.IsNull(fileProcess.FileName); Assert.AreEqual("DocumentProcessor service is not responding. Please contact your administrator to make sure it is running and 'File Processor URI' is set correcltly in the Control Panel.", vm.Message); Assert.AreEqual(string.Format(@"System.Net.WebException{0}Host not responding{0}at ..., at ...", Environment.NewLine), privateAccessor.GetProperty("ExceptionMessage")); Assert.AreEqual(FileControlMode.NotifyFail, vm.CurrentState); // arrange fileProcess.OriginalFileName = "1.doc"; fileProcess.FileName = Guid.NewGuid() + ".doc"; vm.Message = null; privateAccessor.SetProperty("ExceptionMessage", null); vm.CurrentState = FileControlMode.Download; lavel3WcfErrorInfo.ExceptionTypeName = typeof(FileNotFoundException).FullName; lavel3WcfErrorInfo.Message = @"File D:\ImportantFiles\1.doc not exists"; lavel3WcfErrorInfo.StackTrace = "at ..., at ..."; // act vm.OnUploadFinished(null, null); // assert Assert.IsFalse(saveWasCalled); Assert.IsNull(fileProcess.OriginalFileName); Assert.IsNull(fileProcess.FileName); Assert.AreEqual(string.Format(@"Uploaded file in temp location was not found{0}Please verify System Options\System Paths", Environment.NewLine), vm.Message); Assert.AreEqual(string.Format(@"System.IO.FileNotFoundException{0}File D:\ImportantFiles\1.doc not exists{0}at ..., at ...", Environment.NewLine), privateAccessor.GetProperty("ExceptionMessage")); Assert.AreEqual(FileControlMode.NotifyFail, vm.CurrentState); }
public void StartExternalDataServiceTest_when_isStartedHidDeviceFalse() { var root = Mock.Create<IEditableRoot>(); var inst = new PrivateAccessor(this); inst.SetField("isStartedHidDevice", false); var dynamicTypeManager = Mock.Create<IDynamicTypeManager>(Behavior.Loose); var called = false; Mock.Arrange(() => dynamicTypeManager.GetExternalDataInfoList(Arg.IsAny<string>())).DoInstead(() => called = true); Mock.Arrange(() => this.ManagerPortListner(Arg.IsAny<List<ExternalDataDefinition>>())).IgnoreInstance(); Mock.Arrange(() => ThreadPool.QueueUserWorkItem(Arg.IsAny<WaitCallback>())).IgnoreInstance(); Mock.Arrange(() => Application.Current.HasElevatedPermissions).IgnoreInstance().Returns(true); //logger var logger = Mock.Create<ILogger>(Behavior.Loose); Mock.Arrange(() => logger.Log(LogSeverity.Error, typeof(FieldFileViewModel).FullName, Arg.IsAny<Exception>())).IgnoreInstance(); Logger = logger; //TheDynamicTypeManager TheDynamicTypeManager = new Lazy<IDynamicTypeManager>(() => dynamicTypeManager); StartExternalDataService("test", root); Assert.IsNotNull(inst.GetProperty("ProcessName")); Assert.IsTrue(ReferenceEquals(inst.GetProperty("ProcessName"), "test")); }