我有一个单元测试,它为曾经使用 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();
}
}
被测代码:
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;
}
}