c# - How do I write a unit test for this method in my view model in Silverlight without SecurityException? -
I have a Silverlight Testing Project.
I want to test a method in my visual model that takes the FileInfo object when I manually test it through the UI then it works fine.
Now I just want to test a unit for AddDocument method. I really do not want to simulate clicking on button or clicking on a button - I just want to check that addDocument method is there.
Here the code behind the view is in the MySessionViewModel object in DataContext. The method I want to test is mySessionViewModel.AddDocument ();
Private Zero Button. Link (object sender, routing event e) {OpenFileDialog openFileDialog1 = new openfile dialog (); OpenFileDialog1.Filter = "Text Files (.txt) | * .txt | All Files (*. *) * *. *"; OpenFileDialog1.FilterIndex = 1; OpenFileDialog1.Multiselect = True; Bool? UserClickedOK = openFileDialog1.ShowDialog (); If (userClickedOK == true) {IList & lt; FileInfo & gt; Files = New list & lt; FileInfo & gt; (); Foreign (fileinfo file open file Diagnosis 1.Files) {mySessionViewModel.AddDocument (file); }}}
I put some test files in a subdirectory of the web project and tried it, but it constantly throws a security exposure with SilverEle's security model:
< Pre> session view view ViewModel = see new sessionModel (); DirectoryInfo di = new DirectoryInfo ("testFiles"); Var file = Dai EnumerateFile (); Foreign file (file) {sessionViewModel.AddDocument (file); } // Insert some stuff
The main problem here is that you want to test the unit method which will be used as a parameter < Code> FileInfo , although there is no automated way to create an example of FileInfo
.
So to test this method, you should consider changing the type of parameter. There is a great possibility that the internal of this method only use a handful of properties and methods on FileInfo
. Therefore, build the interface that represents those members and change the method to use the interface directly from FileInfo
.
Now you apply the FileInfo
interface and you can also create another class for use in unit testing which applies the same interface but its content gets somewhere else is.
Comments
Post a Comment