All,
I am sure this is covered somewhere, but none of the information I
have found seems to drive it home for me, I can be a little slow at
times :)
Below is a simple example I am working on to better understand the
interactions between Mocks and dependency injection. I am able to use
Rhino.Mocks and successfully mock service classes and the like with no
issue. However, I am attempting to mock the file system for an
application I am working on and I can't quite see how to inject the
filesysteminfo into the test. Here I have just created a test
demonstrating how I would currently write a unit test in this
scenario. I am just trying to fill in the gap on how I would remove
this dependecy on the FileInfo Object. I imagine I need to pass an
interface for the file system into the constructor of MyFileSystem,
but I don't know how to turn that interface back into the actual file
system.
Any guidance would be tremendously appreciated.
namespace Examples{
public class MyFileSystem {
private FileInfo _fileInfo;
public void ArchiveFile(string sourceFilePath,string
archiveFilePath){
_fileInfo =new FileInfo(sourceFilePath);
_fileInfo.CopyTo(archiveFilePath);
_fileInfo.Delete();
}
}
[TestClass]
public class MyFileSystemTest {
[TestMethod]
public void ArchiveFileTest() {
// Arrange
string sourceFilePath = @"c:\sourceFile.txt";
string archiveDirectory = @"c:\archives";
string archiveFilePath = string.Format
(@"{0}\archiveFile.txt",archiveDirectory);
File.Create(sourceFilePath).Close();
Directory.CreateDirectory(archiveDirectory);
// Act
MyFileSystem myFileSystem = new MyFileSystem();
myFileSystem.ArchiveFile(sourceFilePath,archiveFilePath);
// Assert
Assert.IsTrue(File.Exists(archiveFilePath));
Assert.IsFalse(File.Exists(sourceFilePath));
// Tear Down
Directory.Delete(archiveDirectory,true);
}
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Rhino.Mocks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/RhinoMocks?hl=en
-~----------~----~----~----~------~----~------~--~---