How to taunt with DragEventArgs

I am trying to use unit test this method, which is called from my ViewModel:

public virtual string[] ExtractFilePaths(DragEventArgs dragEventArgs) { string[] droppedPaths = null; if (dragEventArgs.Data.GetDataPresent(DataFormats.FileDrop)) { droppedPaths = dragEventArgs.Data.GetData(DataFormats.FileDrop, true) as string[]; } return droppedPaths; } 

I have this method connected to Caliburn. I know this is a fairly simple method that uses infrastructure classes almost exclusively, but I feel that it still needs testing. The problem is that Moq cannot mock DragEventArgs. Is there any way around this or I just don't want to test this method?

+4
source share
2 answers

Hope I don't miss anything with Calibrun, but why mock DragEventArgs when you can create it? An important part is the IDataObject part, which is an interface and can be easily crafted.

 [Test] public void ExtractFilePaths_WithFileDrop_ReturndDropPaths() { var fileList = new[] {@"c:\path\path\file1.txt", @"d:\path2\path2\file2.txt"}; var stubData = Mock.Of<IDataObject>(x => x.GetDataPresent(DataFormats.FileDrop) == true && x.GetData(DataFormats.FileDrop, true) == fileList); var dragEventArgs = new DragEventArgs(stubData, 0, 0, 0, DragDropEffects.Move, DragDropEffects.Scroll); var subject = new Subject(); // Act var result = subject.ExtractFilePaths(dragEventArgs); // Assert Assert.That(result, Is.Not.Null, "Expected array to be returned"); Assert.That(result, Is.EquivalentTo(fileList)); } 
+3
source

Replace the DragEventArgs class with only the data that you want to use in your function. DragEventArgs belongs to the UI, not the ViewModel.

+2
source

Source: https://habr.com/ru/post/1433443/


All Articles