chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hi:

i use AAA to mock a asp.net FileUpload control.

i need to use typemock isolator mock FileUpload.PostedFile.SaveAs() method and throw a exception i use the following code fragment:

Exception myException = new Exception("this is a fake exception");

fakeFileCtrl = Isolate.Fake.Instance<FileUpload>(Members.ReturnRecursiveFakes);

Isolate.WhenCalled(() => fakeFileCtrl.PostedFile.ContentLength).WillReturn(90);

Isolate.WhenCalled(() => fakeFileCtrl.PostedFile.FileName).WillReturn("abc.jpg");

Isolate.WhenCalled(() => fakeFileCtrl.PostedFile.SaveAs("c:\inetpub\wwwroot\MyPrefix.jpg")).WillThrow(myException);


i found no exception thrown

do i miss anything?
asked by lzd (1.2k points)

2 Answers

0 votes
Hi
It looks like we have a bug when using recursive fakes + chains + WillThrow.

The work around is to to record expectation on HttpPostedFile that is returned from PostedFile property separately.

Exception myException = new Exception("this is a fake exception");

var fakeFileCtrl = Isolate.Fake.Instance<FileUpload>(Members.ReturnRecursiveFakes);
var fakePostedFile = Isolate.Fake.Instance<HttpPostedFile>();
//set the fakes on HttpPostedFile
Isolate.WhenCalled(() => fakePostedFile.ContentLength).WillReturn(90);
Isolate.WhenCalled(() => fakePostedFile.FileName).WillReturn("abc.jpg");
Isolate.WhenCalled(() => fakePostedFile.SaveAs("c:\inetpub\wwwroot\MyPrefix.jpg")).WillThrow(myException);
//set FileUpload.PostedFile to return our fake HttpPostedFile
Isolate.WhenCalled(() => fakeFileCtrl.PostedFile).WillReturn(fakePostedFile);


:arrow: The workaround is not inline with the idea of recursive fakes which comes to save you the extra code.
I will update as soon as we will fix the bug.
answered by ohad (35.4k points)
0 votes
OK. thanks. by the way. my typemock version is 5.1.1.
answered by lzd (1.2k points)
...