I'm new here and hope you can help me out. I have a WPF application, here is the method that gets the object (TreeView in this case) and ItemSelectionChangedEventArgs (where I catch the current folder).
public static ObservableCollection<Folder> OnSelectionChanged(object sender, ItemSelectionChangedEventArgs itemSelection)
{
if (sender == null)
{
throw new ArgumentNullException("Can't be null", nameof(sender));
}
if (itemSelection == null)
{
throw new ArgumentNullException("Can't be null", nameof(itemSelection));
}
var selected = itemSelection.AddedItems[0] as Folder;
DirectoryInfo dirInfo = new DirectoryInfo(selected.FullName.ToString());
ObservableCollection<Folder> directories = new ObservableCollection<Folder>();
directories = AddDirectoryInfo(directories, dirInfo);
directories = AddFileInfo(directories, dirInfo);
return directories;
}
I need to write unit test for this method, but than i trying to initialize parametrs for transmitting into this methods i have exeptions.
private readonly string _folderName = "C:\\NewTestFolder";
[TestMethod()]
public void OnSelectionChangedTest()
{
var sender = new SfTreeView() { Name = "treeSize" };
var itemSelection = new ItemSelectionChangedEventArgs()
{
AddedItems = { new Folder()
{
FullName = _folderName,
HasChildrenNodes = false,
Modified = DateTime.Now.ToString(),
Name = "NewTestFolder",
Size = "0",
Type = "Folder"
} },
RemovedItems = { new Folder() { FullName = "" } }
};
var result = FolderView.OnSelectionChanged(sender, itemSelection);
}
sender gives an error: System.InvalidOperationException: 'The calling thread should be STA, since most UI components require it.'
itemSelection gives an error: System.NullReferenceException: 'Object reference not set to an instance of an object.'
Maybe I'm doing something wrong and these parameters to pass into the method should be set in some other way.
CodePudding user response:
First of all, thanks @CharlesMager for answer, i really missed that fact what i don't need "sender" parametr.
For this case, it helped me next, i create new class ItemSelectionChangedEventArgs and inheriting him from current basic class and after that I redefine the variables AddedItems and RemoveItems, because in basic class they have "internal set" and after that i initialize new List.
AddedItems = new List<object>(){ new Folder()
{
FullName = _folderName,
HasChildrenNodes = false,
Modified = DateTime.Now.ToString(),
Name = "NewTestFolder",
Size = "0",
Type = "Folder"
} },
RemovedItems = new List<object>(){ new Folder() }