-
I use Visual Studio 2008 Professional automated tests. I have a function that writes to a file. I want to unit test the file writing function. I have read somewhere that I would have to mock a file somehow. I don’t know how to do it. Can you help?
-
How to unit-test a method that downloads a page from the Internet?
I use Visual Studio 2008 Professional automated tests. I have a function that writes
Share
It depends how close your code is to the nuts’n’bolts; for example, you could work in
Streams instead, and pass aMemoryStreamto the code (and check the contents). You could just write to the file-system (in the temp area), check the contents and ditch it afterwards. Of if your code is a bit above the file system, you could write a mockableIFileSysteminterface with the high-level methods you need (likeWriteAllBytes/WriteAllText). It would be a pain to mock the streaming APIs, though.For downloading from the internet (or pretending to)… you could (for example) write an
IWebClientinterface with the functions you need (likeDownloadString, etc); mock it to return fixed content, and use something likeWebClientas the basis for an actual implementation. Of course, you’ll need to test the actual implementation against real sites.