模拟服务方法,需要同时打开两个流

时间:2015-02-23 14:43:55

标签: c# .net nunit moq

我正在尝试Moq一种需要2个流的方法。 IE:StreamReader和StreamWriter。但是,当我运行我的测试时,我似乎无法通过编译器抛出的异常。每次运行测试时,我都会遇到System.IO.IOException,因为另一个进程正在使用steam。

public void LoadStockLevels(string reportId)
{
    //read file line by line
    CreateRequiredFiles(reportId);
    _updateItemList = new List<IStockLevelItem>();
    _updateAttributeItemList = new List<AttributeItem>();

    try
    {
        _sr = _streamReaderWrapper.GetStreamReader(_report + reportId + ".xml");
        while ((_line = _sr.ReadLine()) != null)
        {
            //sync stock levels
            var stockItem = _line.Split('\t');
            if (stockItem[0] != "sku")
            {
                _updateItemList.Add(new StockLevelItem()
                {
                    Identifier = stockItem[1],
                    Sku = stockItem[0],
                    Quantity = stockItem[3]
                });
                _updateAttributeItemList.Add(new AttributeItem()
                {
                    AttributeValue = stockItem[1],
                    AttributeKey = "IDENT"
                });
            }
            else
            {
                _wsr = _streamWriterWrapper.GetStreamWriter(_inventoryFile, true);
                _wsr.WriteLine(
                    "sku    price   minimum-seller-allowed-price    maximum-seller-allowed-price    quantity    leadtime-to-ship");
                _wsr.Close();
            }
        }
        _sr.Close();
    }
    catch (Exception ex)
    {
        if(_sr != null) _sr.Close();
        if (_wsr != null) _wsr.Close();
        _log.Error("Failed to load stock levels", ex);
    }
}

单元测试:

[Test]
public void Test_LoadStockLevels_CatchesException()
{
    using (var stream1 = new MemoryStream())
    {
        using (var bmp1 = new Bitmap(1, 1))
        {
            var graphics1 = Graphics.FromImage(bmp1);
            graphics1.FillRectangle(Brushes.Black, 0, 0, 1, 1);
            bmp1.Save(stream1, ImageFormat.Jpeg);
        }
        _streamWriterWrapper.Setup(s => s.GetStreamWriter(It.IsAny<string>(), It.IsAny<bool>()))
        .Returns(new StreamWriter(stream1.ToString(), true));

    }

    using (var stream2 = new MemoryStream())
    {
        using (var bmp2 = new Bitmap(1, 1))
        {
            var graphics2 = Graphics.FromImage(bmp2);
            graphics2.FillRectangle(Brushes.Black, 0, 0, 1, 1);
            bmp2.Save(stream2, ImageFormat.Jpeg);
        }
        _streamReaderWrapper.Setup(s => s.GetStreamReader(It.IsAny<string>()))
            .Returns(new StreamReader(stream2.ToString()));
        // Assert something with postedFile here 
        _stockSyncService = new StockSyncService(_stockSync.Object, _tService.Object, _features.Object,
            _macduffClient.Object, _configManager.Object, _streamReaderWrapper.Object, _streamWriterWrapper.Object);
        var result = _stockSyncService.LoadStockLevels("504978");
        Assert.IsInstanceOf<StreamWriter>(result);
    }
}

我知道嘲笑流的问题已得到解答,但我不确定是否已经问过这个问题。

是否可以模拟使用带有内存流的2个流所需的测试。

/ **编辑:添加了StackTrace ** /

Test 'StockIntergration.Services.Tests.StockSync.StockSyncServiceTests.Test_LoadStockLevels_CatchesException' failed:
    System.IO.IOException : The process cannot access the file 'C:\Users\ianrichards\Documents\Repositories\StockIntergration\StockIntergration.Services.Tests\bin\Debug\System.IO.MemoryStream' because it is being used by another process.
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
    at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
    at System.IO.StreamReader..ctor(String path)
    StockSync\StockSyncServiceTests.cs(199,0): at StockIntergration.Services.Tests.StockSync.StockSyncServiceTests.Test_LoadStockLevels_CatchesException()

0 passed, 1 failed, 0 skipped, took 3.71 seconds (NUnit 2.6.2).

2 个答案:

答案 0 :(得分:0)

不使用,请尝试此测试。处理流可能存在问题。删除使用并在测试结束时手动关闭并处理流。

答案 1 :(得分:0)

以下代码是罪魁祸首:

     new StreamWriter(stream1.ToString(), true))
大多数类上的

ToString返回类名,它正是异常显示的内容 - new StreamReader("MemoryStream", true)尝试在当前文件夹中创建具有给定名称的文件并失败。

您正在寻找创建第一个字节数组的另一个MemoryStream

new StreamWriter(new MemoryStream(stream1.ToArray(), false)));