How to create a stub for system.drawing.image?

357 views
Skip to first unread message

Li Aishen

unread,
Mar 23, 2010, 12:27:42 PM3/23/10
to Rhino.Mocks
I am new to unit testing and rhino mock. I want to create a stub for a
system.drawing.image in VB. Can anyone help me on that?
This is the function that needs to be unit tested:
Public Function getSystemDrawingImage(ByVal filePath As String) As
System.Drawing.Image Implements IMyImage.getSystemDrawingImage
If isFileExist(filePath) Or isImageFile(filePath) Then
Dim image As System.Drawing.Image =
System.Drawing.Image.FromFile(filePath)
Return image
Else
Return Nothing
End If
End Function

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

Patrick Steele

unread,
Mar 23, 2010, 8:41:41 PM3/23/10
to rhino...@googlegroups.com
The Width and Height properties in System.Drawing.Image are not
virtual and therefore can not be mocked. However, I'm not sure you
really need to mock out the exact width/height of the image. It looks
like what you want to do is make sure that if isFileExist() returns
true and isImageFile returns true, an image will be loaded. If that's
the case, you may want to consider introducing an interface that would
represent an image loader (since you don't want to tie your image
loading directly to loading from the filesystem -- it's be easier to
mock without that tight coupling).

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.
>
>

Reply all
Reply to author
Forward
0 new messages