即使未使用TextWriter,Assert也会抛出ObjectDisposedException

时间:2012-11-29 00:20:55

标签: unit-testing visual-studio-2012 mbunit

我有一个单元测试,它为一个曾经使用TextWriter的方法抛出了ObjectDisposedException,但不再是。我已经尝试清理解决方案并重建,但似乎无法弄清楚为什么我仍然得到例外。详细说明:

Test method KissMyKindle.Test.Paperwhite520PlusClippingsParserTest.ClipToXmlTest threw exception: 
System.ObjectDisposedException: Cannot write to a closed TextWriter.
Result StackTrace:  
at System.IO.__Error.WriterClosed()
   at System.IO.StringWriter.Write(Char[] buffer, Int32 index, Int32 count)
   at Microsoft.VisualStudio.TestPlatform.MSTestFramework.ThreadSafeStringWriter.Write(Char[] buffer, Int32 index, Int32 count)
   at System.IO.TextWriter.WriteLine(String value)
   at System.IO.TextWriter.SyncTextWriter.WriteLine(String value)
   at Gallio.Common.Markup.TextualMarkupDocumentWriter.StreamBeginSectionImpl(String streamName, String sectionName) in c:\Server\Projects\MbUnit v3\Work\src\Gallio\Gallio\Common\Markup\TextualMarkupDocumentWriter.cs:line 107
   at Gallio.Common.Markup.MarkupDocumentWriter.StreamBeginSection(String streamName, String sectionName) in c:\Server\Projects\MbUnit v3\Work\src\Gallio\Gallio\Common\Markup\MarkupDocumentWriter.cs:line 492
   at Gallio.Common.Markup.MarkupStreamWriter.BeginSection(String sectionName) in c:\Server\Projects\MbUnit v3\Work\src\Gallio\Gallio\Common\Markup\MarkupStreamWriter.cs:line 285
   at Gallio.Framework.Assertions.AssertionFailure.WriteTo(MarkupStreamWriter writer) in c:\Server\Projects\MbUnit v3\Work\src\Gallio\Gallio\Framework\Assertions\AssertionFailure.cs:line 136
   at Gallio.Framework.Assertions.AssertionContext.Scope.LogFailure(AssertionFailure failure) in c:\Server\Projects\MbUnit v3\Work\src\Gallio\Gallio\Framework\Assertions\AssertionContext.cs:line 269
   at Gallio.Framework.Assertions.AssertionContext.SubmitFailure(AssertionFailure failure) in c:\Server\Projects\MbUnit v3\Work\src\Gallio\Gallio\Framework\Assertions\AssertionContext.cs:line 108
   at Gallio.Framework.Assertions.AssertionHelper.Fail(AssertionFailure failure) in c:\Server\Projects\MbUnit v3\Work\src\Gallio\Gallio\Framework\Assertions\AssertionHelper.cs:line 107
   at Gallio.Framework.Assertions.AssertionHelper.Verify(Func`1 assertionFunc) in c:\Server\Projects\MbUnit v3\Work\src\Gallio\Gallio\Framework\Assertions\AssertionHelper.cs:line 93
   at MbUnit.Framework.Assert.AreEqual[T](T expectedValue, T actualValue, EqualityComparison`1 comparer, String messageFormat, Object[] messageArgs) in c:\Server\Projects\MbUnit v3\Work\src\MbUnit\MbUnit\Framework\Assert.Comparisons.cs:line 108
   at MbUnit.Framework.Assert.AreEqual[T](T expectedValue, T actualValue, String messageFormat, Object[] messageArgs) in c:\Server\Projects\MbUnit v3\Work\src\MbUnit\MbUnit\Framework\Assert.Comparisons.cs:line 52
   at MbUnit.Framework.Assert.AreEqual[T](T expectedValue, T actualValue) in c:\Server\Projects\MbUnit v3\Work\src\MbUnit\MbUnit\Framework\Assert.Comparisons.cs:line 38
   at KissMyKindle.Test.Paperwhite520PlusClippingsParserTest.ClipToXmlTest() in d:\projects\Kindle\KissMyKindle\KissMyKindle.Test\Paperwhite520PlusClippingsParserTest.cs:line 78

测试代码:

[TestMethod]
public void ClipToXmlTest()
{
    const string EXPECTED = "";
    const string TEST_FILE_NAME = "MyTestFile";
    var mockFile = MockRepository.GenerateMock<IFile>();
    var mockIoFactory = MockRepository.GenerateMock<IIoFactory>();
    mockFile.Expect(x => x.Exists).Return(true);
    mockIoFactory.Expect(x => x.GetFile(TEST_FILE_NAME)).Return(mockFile);
    using (var stream = new MemoryStream(Encoding.Default.GetBytes(CLIPPINGS_DATA_SAMPLE)))
    {
        mockFile.Expect(x => x.OpenRead()).Return(stream);

        var actual = new KindleClippingsParser(mockIoFactory).ClipToXml(TEST_FILE_NAME).ToString();
        Assert.AreEqual(EXPECTED, actual); // fails here

        mockFile.VerifyAllExpectations();
        mockIoFactory.VerifyAllExpectations();
    }
}

受测试代码:

公共类KindleClippingsParser {     // ...     public IEnumerable Clippings(string path)     {         if(string.IsNullOrEmpty(path))抛出新的ArgumentNullException(“path”);

    var file = _ioFactory.GetFile(path);
    if (!file.Exists) throw new FileNotFoundException("Failed to open the clippings file.", path);

    _lineNumber = 0;
    using (var reader = new StreamReader(file.OpenRead()))
    {
        var data = new List<string>();
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            if (line.StartsWith(HighlightSeparator))
            {
                yield return ParseHighlight(data);
                data.Clear();
            }
            else
            {
                data.Add(line);
            }
        }
    }
}

public XElement ClipToXml(string inputPath)
{
    var rootEl = new XElement("MyClippings");
    foreach (var clippingGroup in Clippings(inputPath).GroupBy(c => c.Title))
    {
        var bookEl = new XElement("Book", new XElement("Title", clippingGroup.Key));
        var isFirst = true;
        foreach (var highlight in clippingGroup)
        {
            if (isFirst)
            {
                bookEl.Add(new XElement("Author", highlight.Author));
                isFirst = false;
            }
            var highlightEl = new XElement("KindleHighlight", new XAttribute("location", highlight.Location),
                                                              new XAttribute("added", highlight.Added.ToShortDateString()));
            highlightEl.Value = highlight.Text;
            bookEl.Add(highlightEl);
        }
        rootEl.Add(bookEl);
    }

    return rootEl;
}

}

1 个答案:

答案 0 :(得分:0)

Doh ......修好了。原来它试图使用MISSit Assert和MSTest框架;我最初创建了MbUnit的测试类,只是在我意识到我当时无法在Visual Studio 2012中运行MbUnit测试时切换到MSTest。

为了让事情顺利进行,我所要做的就是删除以下行:

using Assert = MbUnit.Framework.Assert;