Here is the unit testing I want to write:
Public Sub
getSystemDrawingImageTest_Using_Mock_If_File_Path_Is_Valid_Return_Image()
Dim target As MyImage = New MyImage()
Dim filePath As String = "Fake_file_Path.jpg"
Dim expectedWidth As Integer = 1024
Dim expectedHeight As Integer = 768
Dim actual As Image
Dim mocks = New MockRepository
Dim stub = mocks.Stub(Of IMyImage)()
Dim img As System.Drawing.Image
Using mocks.Record
stub.isFileExist(filePath)
LastCall.Return(True)
stub.isImageFile()
LastCall.Return(True)
' how to set return image.Width and image.Height value as
expectedWidth and expectedHeight?
End Using
actual = target.getSystemDrawingImage(filePath)
Assert.AreEqual(expectedWidth, actual.Width)
Assert.AreEqual(expectedHeight, actual.Height)
End Sub
Thank you for your help
Best Regards
Li Aishen
I'm very rusty on VB.NET so I'll show some C# code of what you might
want to consider.
First, an image loader that loads an Image:
interface IImageLoader
{
Image Load();
}
Currently, it looks like you only support loading image from a file on
disk so you can create a file-based IImageLoader:
public class FileImageLoader : IImageLoader
{
private string filename;
public FileImageLoader(string filename)
{
this.filename = filename;
}
public Image Load()
{
return Image.FromFile(filename);
}
}
Now you can change getSystemDrawingImage to accept an IImageLoader.
In production, you would create a new FileImageLoader and pass it the
name of the file. For unit testing (and mocking), you could mock out
IImageLoader to return a pre-defined image of some type. Also, your
getSystemDrawingImage method is now more flexible as it can easily be
changed to load images from somewhere else (memory, web, database), by
simply implementing a new IImageLoader.
Also, the isFileExists() and isImageFile() methods need to be virtual
if you want to mock them out.
Let me know if this helps you out at all.
---
Patrick Steele
http://weblogs.asp.net/psteele
> --
> You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
> To post to this group, send email to rhino...@googlegroups.com.
> To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rhinomocks?hl=en.
>
>