I have a Silverlight testing project using the Silverlight Unit Test Framework.
I want to test a method in my view model which takes FileInfo objects. This seems to work fine when I test it manually through the UI.
Now I want a unit test just for the AddDocument method. I don't want to test actually clicking the button or simulate clicking a button - I just want to test that the AddDocument method.
Here's the code behind from the view. The mySessionViewModel object is in the DataContext. The method I want to test is mySessionViewModel.AddDocument();
private void Button_Click_1(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter =
"Text Files (.txt)|*.txt|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = true;
bool? userClickedOK = openFileDialog1.ShowDialog();
if (userClickedOK == true)
{
IList<FileInfo> files = new List<FileInfo>();
foreach (FileInfo file in openFileDialog1.Files)
{
mySessionViewModel.AddDocument(file);
}
}
}
I put some test files in a subdirectory of the web project and tried this, but it throws a SecurityException consistent with Silverlight's security model:
SessionView sessionViewModel = new SessionViewModel();
DirectoryInfo di = new DirectoryInfo("testFiles");
var files = di.EnumerateFiles();
foreach (var file in files)
{
sessionViewModel.AddDocument(file);
}
// assert some stuff