Click here to Skip to main content
15,354,641 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The sourceFile will pass in the path of the file and in destination which is the copy funciton will copy the sourceFile to it.

public bool CopyFile(string sourceFile, string destinationFile)
    if (File.Exists(sourceFile))
        File.Copy(sourceFile, destinationFile, true);
        return true;
        return false;


What I have tried:

I have watch the sample from internet resources but still no idea about it.
Updated 20-Jun-19 1:02am
lmoelleb 20-Jun-19 6:48am
I am not sure what you are asking? 1) How to write any test? 2) How to run the test? 3) How to test this specific method (meaning you can already run a dummy test that does nothing)
MAN CHUN LIEW 20-Jun-19 6:51am
sorry for the confusing title. I want to write a test by using the visual studio framework but im not sure how to write it to test the above example.
lmoelleb 20-Jun-19 6:54am
I gave you a list of 3 options to choose from to tell us where you are stuck. We do not want to waste our time telling you something, just to be told you already know that part. So please update your question with details based on these three options.
MAN CHUN LIEW 20-Jun-19 6:58am

1 solution

That code can't be unit tested. What is it you're actually testing? File.Exists? File.Copy? Those are third-party functions, you should just assume that they work. To make that code testable you'll need to create some kind of IFileManagement service that has Exists and Copy methods on it. In your actual code you will use a class that implements this interface and uses the System.IO functions to make it work. In your unit test you will use a mocked version of IFileManagement that you configure to either return true or false, and you then check that if Exists returns true that Copy is called, and that if Exists returns false, Copy is not called. You would also test that when Copy is called it is passed the right parameters too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900