如何从 RhinoMocks 解决 System.InvalidOperationException?
How to solve System.InvalidOperationException from RhinoMocks?
出于测试目的,我使用了 NUnit 和 RhinoMocks。昨天发生错误,我无法解决。我在互联网上搜索了解决方案。我找到了很多可能的解决方案,但就我而言,它并没有解决问题。
异常堆栈跟踪:
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()
代码:
[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);
}
}
有人知道我该如何解决这个错误吗?
非常感谢。
乔迪
您的 ImageHandler 需要另一个模拟,就像这样:
[TestFixture]
public class ImageControllerTest
{
[Test]
public void SmokeTest()
{
var container = MockRepository.GenerateStrictMock<IContainer>();
var handler = MockRepository.GenerateStrictMock<IImageHandler>();
var imageController = new ImageController(container);
string imagePhysicalLocation = @"c:\image.jpg";
Stream imageStream = null;
container.Expect(x => x.GetImageHandler()).Return(handler);
handler.Expect(x => x.LoadImage(imagePhysicalLocation)).Repeat.Once().Return(imageStream);
handler.Expect(x => x.ConvertToGrayScale(imageStream)).Repeat.Once().Return(imageStream);
var dimensions = new KeyValuePair<int, int>(200, 200);
handler.Expect(x => x.GetDimensions(imageStream)).Repeat.Once().Return(dimensions);
imageController.Execute(imagePhysicalLocation);
container.VerifyAllExpectations();
handler.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);
}
}
祝你好运!
出于测试目的,我使用了 NUnit 和 RhinoMocks。昨天发生错误,我无法解决。我在互联网上搜索了解决方案。我找到了很多可能的解决方案,但就我而言,它并没有解决问题。
异常堆栈跟踪:
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()
代码:
[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);
}
}
有人知道我该如何解决这个错误吗?
非常感谢。
乔迪
您的 ImageHandler 需要另一个模拟,就像这样:
[TestFixture]
public class ImageControllerTest
{
[Test]
public void SmokeTest()
{
var container = MockRepository.GenerateStrictMock<IContainer>();
var handler = MockRepository.GenerateStrictMock<IImageHandler>();
var imageController = new ImageController(container);
string imagePhysicalLocation = @"c:\image.jpg";
Stream imageStream = null;
container.Expect(x => x.GetImageHandler()).Return(handler);
handler.Expect(x => x.LoadImage(imagePhysicalLocation)).Repeat.Once().Return(imageStream);
handler.Expect(x => x.ConvertToGrayScale(imageStream)).Repeat.Once().Return(imageStream);
var dimensions = new KeyValuePair<int, int>(200, 200);
handler.Expect(x => x.GetDimensions(imageStream)).Repeat.Once().Return(dimensions);
imageController.Execute(imagePhysicalLocation);
container.VerifyAllExpectations();
handler.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);
}
}
祝你好运!