首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于密封PngBitmapEncoder的伪造流

用于密封PngBitmapEncoder的伪造流
EN

Stack Overflow用户
提问于 2018-04-26 14:39:00
回答 2查看 219关注 0票数 0

这是带有I/O依赖项的单元测试的后续问题。

如何使 PngBitmapEncoder 接受包装/模拟的FileStream?

在BitmapService.SaveBitmapAsPngImage()中,我想断言位图编码器调用流保存: bitmapEncoder.Save(outStream.StreamInstance);

需要对FileStream进行“有效”PngBitmapEncoder的犀牛模拟测试:

代码语言:javascript
复制
[Test]
public void BitmapService_Should_SaveBitmapAsPngImage_RhinoMocks()
{
    // Arrange
    IFile fileMock = MockRepository.GenerateStrictMock<IFile>();
    IFileStream fileStreamWrapperMock = MockRepository.GenerateStub<IFileStream>();
    fileMock.Expect(x => x.Open(string.Empty, FileMode.OpenOrCreate))
        .IgnoreArguments().Return(fileStreamWrapperMock);
    var bitmapEncoderFactory = MockRepository.GenerateStub<IBitmapEncoderFactory>();

    PngBitmapEncoder pngBitmapEncoder = new PngBitmapEncoder();
    bitmapEncoderFactory.Expect(x => x.CreateBitmapEncoder()).Return(pngBitmapEncoder);

    BitmapService sut = new BitmapService(fileMock, new PngBitmapEncoderFactory());
    Size renderSize = new Size(100, 50);
    RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
        (int)renderSize.Width, (int)renderSize.Height, 96d, 96d, PixelFormats.Pbgra32);

    // Act
    sut.SaveBitmapAsPngImage(new Uri("//A_valid_path"), renderBitmap);

    // Assert
    pngBitmapEncoder.AssertWasCalled(x => x.Save(fileStreamWrapperMock.FileStreamInstance));
}

犀牛测试结果:

System.ArgumentNullException :值不能为空。参数名称:流 在System.Windows.Media.StreamAsIStream.IStreamFrom(Stream流) 在System.Windows.Media.Imaging.BitmapEncoder.Save(Stream流) 在BitmapService.SaveBitmapAsPngImage(Uri路径,BitmapSource renderBitmap)

我更喜欢犀牛模型,但这是@Nkosi的Moq测试。不幸的是,它也没有发挥作用:

代码语言:javascript
复制
[TestMethod]
public void BitmapService_Should_SaveBitmapAsPngImage()
{
    //Arrange
    var mockedStream = Mock.Of<Stream>(_ => _.CanRead == true && _.CanWrite == true);
    Mock.Get(mockedStream).SetupAllProperties();
    var fileSystemMock = new Mock<IFileSystem>();
    fileSystemMock
        .Setup(_ => _.OpenOrCreateFileStream(It.IsAny<string>()))
        .Returns(mockedStream);

    var sut = new BitmapService(fileSystemMock.Object);
    Size renderSize = new Size(100, 50);
    var renderBitmap = new RenderTargetBitmap(
        (int)renderSize.Width, (int)renderSize.Height, 96d, 96d, PixelFormats.Pbgra32);
    var path = new Uri("//A_valid_path");

    //Act
    sut.SaveBitmapAsPngImage(path, renderBitmap);

    //Assert
    Mock.Get(mockedStream)
        .Verify(_ => _.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()));
}

Moq测试结果:

BitmapServiceTest.BitmapService_Should_SaveBitmapAsPngImage抛出异常: System.IO.IOException:无法从流中读取。-> System.Runtime.InteropServices.COMException: HRESULT: 0x88982F72在System.Windows.Media.Imaging.BitmapEncoder.Save(Stream流中的异常)

所以这个测试尝试使用那个流。

被测试的类:

代码语言:javascript
复制
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using SystemInterface.IO;
using SystemWrapper.IO;

public interface IBitmapService
{
    void SaveBitmapAsPngImage(Uri path, BitmapSource renderBitmap);
}

public interface IBitmapEncoderFactory
{
    BitmapEncoder CreateBitmapEncoder();
}

public class PngBitmapEncoderFactory : IBitmapEncoderFactory
{
    public BitmapEncoder CreateBitmapEncoder()
    {
        return new PngBitmapEncoder();
    }
}

public class BitmapService : IBitmapService
{
    private readonly IFile _fileWrapper;
    private readonly IBitmapEncoderFactory _bitmapEncoderFactory;

    public BitmapService(IFile fileWrapper, IBitmapEncoderFactory bitmapEncoderFactory)
    {
        _fileWrapper = fileWrapper;
        _bitmapEncoderFactory = bitmapEncoderFactory;
    }

    public void SaveBitmapAsPngImage(Uri path, BitmapSource renderBitmap)
    {
        // Create a file stream for saving image
        using (IStream outStream = _fileWrapper
            .Open(path.LocalPath, FileMode.OpenOrCreate))
        {
            // Use bitmap encoder for our data
            BitmapEncoder bitmapEncoder = _bitmapEncoderFactory.CreateBitmapEncoder();
            // push the rendered bitmap to it
            bitmapEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));

            // The problem: Expects real Stream as parameter!!!
            bitmapEncoder.Save(outStream.StreamInstance);
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-26 14:57:04

正如我在注释中所建议的,如果模拟Stream不适合使用模拟框架,请考虑通过包装一个MemoryStream来创建一个模拟/存根,

代码语言:javascript
复制
public class MockedFileStream : MemoryStream {
    protected override void Dispose(bool disposing) {
        //base.Dispose(disposing);
        //No Op fr the purposes of the test.
    }

    public override void Close() {
        //base.Close();
        //No Op fr the purposes of the test.
    }

    public void CustomDispose() {
        base.Dispose(true);
        GC.SuppressFinalize(this);
    }
}

为你准备好了所有的水管。唯一的问题是,当流与Dispose语句一起使用时,必须重写using方法。

然后,将更新测试以使用“假”流。

代码语言:javascript
复制
[TestMethod]
public void BitmapService_Should_SaveBitmapAsPngImage() {
    //Arrange
    var mockedStream = new MockedFileStream();
    var fileSystemMock = new Mock<ImageDrawingCombiner3.IFileSystem>();
    fileSystemMock
        .Setup(_ => _.OpenOrCreateFileStream(It.IsAny<string>()))
        .Returns(mockedStream);

    var sut = new ImageDrawingCombiner3.BitmapService(fileSystemMock.Object);
    Size renderSize = new Size(100, 50);
    var renderBitmap = new RenderTargetBitmap(
        (int)renderSize.Width, (int)renderSize.Height, 96d, 96d, PixelFormats.Pbgra32);
    var path = new Uri("//A_valid_path");

    //Act
    sut.SaveBitmapAsPngImage(path, renderBitmap);

    //Assert
    mockedStream.Length.Should().BeGreaterThan(0); //data was written to it.

    mockedStream.CustomDispose(); //done with stream
}

在我看来,不应该真的需要模拟或抽象PngBitmapEncoder,因为它是一个实现问题。

票数 1
EN

Stack Overflow用户

发布于 2018-04-28 09:21:38

非常感谢@NKosi带领我们走向正确的方向。为了完整起见,我想发布NUnit,Rhino模拟测试。

代码语言:javascript
复制
[Test]
public void ShouldSaveBitmapAsPngImage()
{
    // Arrange
    Uri pathToFile = new Uri("//A_valid_path");
    IFileSystem fileSystemMock = MockRepository.GenerateStrictMock<IFileSystem>();
    MockedFileStream mockedFileStream = new MockedFileStream();
    fileSystemMock.Expect(x => x.OpenOrCreateFileStream(pathToFile.AbsolutePath))
        .IgnoreArguments().Return(mockedFileStream);

    BitmapService sut = new BitmapService(fileSystemMock);
    Size renderSize = new Size(100, 50);
    RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
        (int)renderSize.Width, (int)renderSize.Height, 96d, 96d, PixelFormats.Pbgra32);

    // Act
    sut.SaveBitmapAsPngImage(pathToFile, renderBitmap);

    // Assert
    // Was data was written to it?
    Assert.That(mockedFileStream.Length, Is.GreaterThan(0));

    mockedFileStream.CustomDispose(); //done with stream
}

并摆脱了服务中的BitmapEncoder抽象:

代码语言:javascript
复制
public class BitmapService : IBitmapService
{
    private readonly IFileSystem _fileSystem;

    public BitmapService(IFileSystem fileSystem)
    {
        _fileSystem = fileSystem;
    }

    public void SaveBitmapAsPngImage(Uri path, BitmapSource renderBitmap)
    {
        // Create a file stream for saving image
        using (Stream outStream = _fileSystem.OpenOrCreateFileStream(path.LocalPath))
        {
            // Use bitmap encoder for our data
            BitmapEncoder bitmapEncoder = new PngBitmapEncoder();
            // push the rendered bitmap to it
            bitmapEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));
            // save the data to the stream
            bitmapEncoder.Save(outStream);
        }
    }
}

我真的很满意这个干净整洁的解决方案。谢谢@NKosi和StackOverflow ;)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50045807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档