Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8924137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:19:45+00:00 2026-06-15T07:19:45+00:00

I have a unit test that is throwing an ObjectDisposedException for a method that

  • 0

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;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:19:46+00:00Added an answer on June 15, 2026 at 7:19 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a unit test that creates a mock calls my method to be
I have a unit test that tests if an Exception is throw, but this
I have this code that works in a unit test but doesn't work when
I have a method CreateAccount(...) that I want to unit test. Basically it creates
I'm trying to unit test a private method that I have attached to my
I have a unit test that is failing and I know why but I'm
I have a unit test that is used to test variable conversions from string
I have a unit test that works fine locally but when uploaded to TeamCity
I have a unit test that fails because headers are already sent. However, the
I have a unit test that I am writing and have run into an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.