0

In my controller I have an ActionResult method, that looks like this:

    public ActionResult Well(string slideid, string path)
    {
        var model = slideRepository.GetWells(slideid);
        var firstPath = slideRepository.MapPath(path);

        byte[] firstImageByteData = System.IO.File.ReadAllBytes(firstPath + "/" + slideid + "_first.jpg");
        string firstImageBase64Data = Convert.ToBase64String(firstImageByteData);
        string firstImageDataUrl = string.Format("data:image/png;base64,{0}", firstImageBase64Data);

        ViewBag.firstImageData = firstImageDataUrl;

        return View("Well", model);
    }

I use the Repository pattern and I've unit tested it successfully up until the model, but suddenly a new requirement has risen where I need to go fetch an image and display it in the view as well. I'm aware that unit testing file IO is something that can be discussed whether it's a good idea or not, but as the method looks right now, I'm not sure how to test it. Any suggestions?

1
  • 2
    Have you thought about abstracting the IO behind an interface and injecting it? Commented Oct 14, 2015 at 9:17

2 Answers 2

1

What you can do is, take out the IO functionalities out of the controller in a separate class, and make that class Interface driven. Next inject the instance to the controller.

Now when writing the unit tests, inject mock object of the IO interface and set the expectations of this mock object.

Sharing some links on details about unit testing and mocking.

https://msdn.microsoft.com/en-us/library/ff650441.aspx

https://github.com/Moq/moq4

http://www.developerhandbook.com/unit-testing/writing-unit-tests-with-nunit-and-moq/

Testing a MVC Controller fails with NULL reference exception

Not only you will be able to unit test your code now, it will lead to a better design as well, remember if you are facing any challenges in unit testing a code, possibly it has scope of design improvement.

Sign up to request clarification or add additional context in comments.

Comments

0

You should use some wrapper with interface to be able to inject like repository. Example:

public class FileSystemProvider : IFileSystemProvider
{
    private readonly IFilesLibraryConfiguration configuration;

    public FileSystemProvider(IFilesLibraryConfiguration configuration)
    {
        this.configuration = configuration;
    }

    public string ReadAllText(string virtualPath)
    {
        var fullPath = VirtualPathToFullPath(virtualPath);

        var allText = File.ReadAllText(fullPath);
        return allText;
    }

    public bool FileExists(string virtualPath)
    {
        var fullPath = VirtualPathToFullPath(virtualPath);

        return File.Exists(fullPath);
    }

    private string VirtualPathToFullPath(string virtualPath)
    {
        return HostingEnvironment.MapPath(virtualPath);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.