문제

I have a unit test that is throwing an ObjectDisposedException for a method that used to be using a TextWriter, but no longer is. I've tried cleaning the solution and rebuilding, but can't seem to figure out why I'm still getting the exception. Details:

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

Test code:

[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();
    }
}

Code under test:

public class KindleClippingsParser { // ... public IEnumerable Clippings(string path) { if (string.IsNullOrEmpty(path)) throw new 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;
}

}

도움이 되었습니까?

해결책

Doh...fixed it. Turns out it was trying to use the Mbunit Assert with the MSTest framework; I had originally created the test class with MbUnit in mind, only to switch to MSTest once I realized that I wasn't able to run the MbUnit tests within Visual Studio 2012 at the time.

To get things working, all I had to do was remove the following line:

using Assert = MbUnit.Framework.Assert;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top