Current filter:
                                You should refresh the page.
                                  • Hi

                                    I am trying to write a unit test for some code that displays another view using IDocumentManagerService and I'm having problems mocking the window to return.

                                    The test looks like this...

                                    [C#]
                                    var documentManagerService = new Mock<IDocumentManagerService>(); documentManagerService.Setup(x => x.CreateDocument(It.IsAny<string>(), It.IsAny<object>(), It.IsAny<object>(), It.IsAny<object>())).Returns((string documentType, object viewModel, object parameter, object parentViewModel) => { documentManagerServiceCalled = true; return new WindowDocument(new WindowedDocumentUIService(), null, null, nameof(LocalAutoDataMatchingView)); }); ((ISupportServices)viewModel).ServiceContainer.RegisterService(documentManagerService.Object);

                                    And the bit of code that it is failing with is...

                                    [C#]
                                    var document = this.DocumentManagerService.CreateDocument(nameof(LocalAutoDataMatchingView), localAutoDataMatchViewModel, null, null); document.Title = "Local Data Matching - Canvass Reform"; document.Show();

                                    Hope you can help
                                    Justin

                                1 Solution

                                Creation Date Importance Sort by

                                Hi Justin,

                                I see NullReferenceException in the WindowDocument class constructor when using this approach. Can you confirm that you experience the same issue?

                                If so, WindowDocument requires the window parameter to be set to a value different from null. In your case, you set it to null, and that is why the issue occurs.

                                I suggest you create a mock for the IDocument interface and return it instead.

                                [C#]
                                documentManagerService.Setup(x => x.CreateDocument(It.IsAny<string>(), It.IsAny<object>(), It.IsAny<object>(), It.IsAny<object>())).Returns((string documentType, object viewModel, object parameter, object parentViewModel) => { //... //return new WindowDocument(new WindowedDocumentUIService(), null, null, nameof(MainWindow)); return new Mock<IDocument>().Object; });

                                Thanks

                                • Customer77670 10.17.2019

                                  Thanks Ivan, works perfectly
                                  Justin