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



我有一个单元测试,它正在为一个曾经使用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:ServerProjectsMbUnit v3WorksrcGallioGallioCommonMarkupTextualMarkupDocumentWriter.cs:line 107
at Gallio.Common.Markup.MarkupDocumentWriter.StreamBeginSection(String streamName, String sectionName) in c:ServerProjectsMbUnit v3WorksrcGallioGallioCommonMarkupMarkupDocumentWriter.cs:line 492
at Gallio.Common.Markup.MarkupStreamWriter.BeginSection(String sectionName) in c:ServerProjectsMbUnit v3WorksrcGallioGallioCommonMarkupMarkupStreamWriter.cs:line 285
at Gallio.Framework.Assertions.AssertionFailure.WriteTo(MarkupStreamWriter writer) in c:ServerProjectsMbUnit v3WorksrcGallioGallioFrameworkAssertionsAssertionFailure.cs:line 136
at Gallio.Framework.Assertions.AssertionContext.Scope.LogFailure(AssertionFailure failure) in c:ServerProjectsMbUnit v3WorksrcGallioGallioFrameworkAssertionsAssertionContext.cs:line 269
at Gallio.Framework.Assertions.AssertionContext.SubmitFailure(AssertionFailure failure) in c:ServerProjectsMbUnit v3WorksrcGallioGallioFrameworkAssertionsAssertionContext.cs:line 108
at Gallio.Framework.Assertions.AssertionHelper.Fail(AssertionFailure failure) in c:ServerProjectsMbUnit v3WorksrcGallioGallioFrameworkAssertionsAssertionHelper.cs:line 107
at Gallio.Framework.Assertions.AssertionHelper.Verify(Func`1 assertionFunc) in c:ServerProjectsMbUnit v3WorksrcGallioGallioFrameworkAssertionsAssertionHelper.cs:line 93
at MbUnit.Framework.Assert.AreEqual[T](T expectedValue, T actualValue, EqualityComparison`1 comparer, String messageFormat, Object[] messageArgs) in c:ServerProjectsMbUnit v3WorksrcMbUnitMbUnitFrameworkAssert.Comparisons.cs:line 108
at MbUnit.Framework.Assert.AreEqual[T](T expectedValue, T actualValue, String messageFormat, Object[] messageArgs) in c:ServerProjectsMbUnit v3WorksrcMbUnitMbUnitFrameworkAssert.Comparisons.cs:line 52
at MbUnit.Framework.Assert.AreEqual[T](T expectedValue, T actualValue) in c:ServerProjectsMbUnit v3WorksrcMbUnitMbUnitFrameworkAssert.Comparisons.cs:line 38
at KissMyKindle.Test.Paperwhite520PlusClippingsParserTest.ClipToXmlTest() in d:projectsKindleKissMyKindleKissMyKindle.TestPaperwhite520PlusClippingsParserTest.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{//。。。公共IEnumerable剪辑(字符串路径){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;
}

}

Doh。。。修复了它。原来它试图将Mbunit Assert与MSTest框架一起使用;我最初创建测试类时考虑到了MbUnit,但当我意识到当时无法在Visual Studio 2012中运行MbUnit测试时,我切换到了MSTest。

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

using Assert = MbUnit.Framework.Assert;

相关内容

  • 没有找到相关文章

最新更新