mercredi 26 août 2015

How to solve System.InvalidOperationException from RhinoMocks?

For testing purposes I'm using NUnit and RhinoMocks. An error occured yesterday and I cannot get it solved. I searched the internet for a solution. I found a lot possible solutions, but in my case it doesn't solve the issue.

The error.

System.InvalidOperationException : Previous method 'IContainer.GetImageHandler();' requires a return value or an exception to throw.

at Rhino.Mocks.Impl.RecordMockState.AssertPreviousMethodIsClose() at Rhino.Mocks.Impl.RecordMockState.Replay() at Rhino.Mocks.MockRepository.ReplayCore(Object obj, Boolean checkInsideOrdering) at Rhino.Mocks.RhinoMocksExtensions.Expect(T mock, Function`2 action) at ImageControllerTest.UnitTests.ImageControllerTest.SmokeTest()

The code.

    [TestFixture]
public class ImageControllerTest
{
    [Test]
    public void SmokeTest()
    {
        var container = MockRepository.GenerateStrictMock<IContainer>();
        var imageController = new ImageController(container);

        string imagePhysicalLocation = @"c:\image.jpg";

        Stream imageStream = null;
        container.Expect(x => x.GetImageHandler().LoadImage(imagePhysicalLocation)).Repeat.Once().Return(imageStream);

        container.Expect(x => x.GetImageHandler().ConvertToGrayScale(imageStream)).Repeat.Once().Return(imageStream);

        var dimensions = new KeyValuePair<int, int>(200, 200);

        container.Expect(x => x.GetImageHandler().GetDimensions(imageStream)).Repeat.Once().Return(dimensions);

        imageController.Execute(imagePhysicalLocation);

        container.VerifyAllExpectations();
    }
}

public interface IContainer
{
    IImageHandler GetImageHandler();
}

public interface IImageHandler
{
    Stream LoadImage(string physicalLocation);
    KeyValuePair<int, int> GetDimensions(Stream image);
    Stream ConvertToGrayScale(Stream image);
}

public class ImageController
{
    private readonly IContainer container;

    public ImageController(IContainer container)
    {
        this.container = container;
    }

    public void Execute(string imagePhysicalLocation)
    {
        Stream image = container.GetImageHandler().LoadImage(imagePhysicalLocation);

        Stream imageInGrayScale = container.GetImageHandler().ConvertToGrayScale(image);

        container.GetImageHandler().GetDimensions(imageInGrayScale);
    }
}

Does anyone have any idea how I could solve this error?

Thanks a lot.

Jordy

Aucun commentaire:

Enregistrer un commentaire