我是个模拟新手,所以我需要一些关于如何用Rhino mock模拟HttpPostedFileBase的指导。我正在尝试验证我的ToByteArray()扩展是否按预期工作,这就是我到目前为止所得到的:
[Test]
public void Should_return_a_byte_array_with_a_length_of_eleven()
{
// Arrange
var stream = new MemoryStream(System.Text.Encoding.Default.GetBytes("TestContent"));
var httpPostedFileBase = MockRepository.GenerateMock<HttpPostedFileBase>();
httpPostedFileBase.Expect(x => x.InputStream).Return(stream);
httpPostedFileBase.Expect(x => x.ContentLength).Return(11);
// Act
var byteArray = httpPostedFileBase.ToByteArray();
// Assert
Assert.That(byteArray.Length, Is.EqualTo(11));
}我可以断定值已经设置好了,但是当我的扩展方法获得HttpPostedFileBase时,它已经丢失了所有的值。任何帮助都将不胜感激。
/ Kristoffer
发布于 2009-06-04 12:20:14
只要有可能,您就应该避免模仿,以便验证您的实现是否符合您的期望。相反,对于某些特定的输入,支持测试,输出是您所期望的。
也就是说,你的例子遗漏了一些关键的东西。当你使用mock时,你需要告诉他们你什么时候完成了期望的设置(否则他们会记录所有的方法调用,比如进一步的期望),方法是调用:
httpPostedFileBase.Replay();最后,在断言阶段,使用以下命令验证您的期望:
httpPostedFileBase.VerifyAllExpectations();还要注意,使用Rhino时,您只能模拟虚拟的方法和属性。
https://stackoverflow.com/questions/950104
复制相似问题