Kirill.Lutzow Kirill.Lutzow - 2 months ago 15
C# Question

Unit Testing using SystemWrapper and Rhino Mocks

I have the method which makes backup of some file:

public void MakeBackup(IFileWrap file, string path)
if (path == null)
throw new ArgumentNullException();


string backups = Environment.CurrentDirectory + @"\Backups\";

if (!Directory.Exists(backups))

if (file.Exists(path))
file.Copy(path,backups + Path.GetFileName(path),overwrite: true);
Console.WriteLine("Backup of the " + Path.GetFileName(path) + " lies in the " + backups);


And Iam trying to test it by using SystemWrapper and Rhino Mocks:

public void MakeBackupTest()
IFileWrap fileRepository = MockRepository.GenerateMock<IFileWrap>();

fileRepository.Expect(x => x.Exists(@"G:\1.txt"));
fileRepository.Expect(x => x.Copy(@"G:\1.txt", Environment.CurrentDirectory + @"\Backups\1.txt", overwrite: true));

new Windows().MakeBackup(fileRepository,@"G:\1.txt");



The test above fails. What am I doing wrong?

Lee Lee

You aren't setting a return value for fileRepository.Exists - the default will be false. It should be something like:

fileRepository.Expect(x => x.Exists(@"G:\1.txt")).Return(true);