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 989663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:49:42+00:00 2026-05-16T05:49:42+00:00

I extend upon a legacy library which accesses files on the harddrive. I have

  • 0

I extend upon a legacy library which accesses files on the harddrive. I have such files as embedded resources in my unit test project.

I have ported parts of the library to accept streams, which allows me to use GetManifestResourceStream to pass my embedded resource to the legacy library. This works fine, but it is a slight hassle. And people maintaining those libraries don’t appreciate the “clutter” or having to publish new releases.

JustMock and TypeMock allows me to intercept the File.Open commmand, and I wish to pass the library a FileStream object, but how do I construct a FileStream Object from an Embedded Manifest Resource?
I could of course create a physical file, but I don’t wish to touch the file system while running tests.

  • 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-05-16T05:49:42+00:00Added an answer on May 16, 2026 at 5:49 am

    I have cooked a sample based on the requirments you mentioned here. I used in memory stream for it but can done with embedded resource as well.

               byte[] actual = new byte[255];
    
            // writing locally, can be done from resource manifest as well.
    
            using (StreamWriter writer = new StreamWriter(new MemoryStream(actual)))
            {
                writer.WriteLine("Hello world");
                writer.Flush();
            }
    
            // arrange the file system.
    
            FileStream fs = (FileStream)FormatterServices
                .GetSafeUninitializedObject(typeof(FileStream));
    
            // mocking the specific call and setting up expectations.
            Mock.Arrange(() => fs.Write(Arg.IsAny<byte[]>(), Arg.AnyInt, Arg.AnyInt))
                .DoInstead((byte[] content, int offset, int len) =>
            {
                actual.CopyTo(content, offset);
            });
    
            // return custom filestream for File.Open.
            Mock.Arrange(() => File.Open(Arg.AnyString, Arg.IsAny<FileMode>()))
                 .Returns(fs);
    
    
            // act
            var fileStream =  File.Open("hello.txt", FileMode.Open);
            byte[] fakeContent = new byte[actual.Length];
    
            // original task
            fileStream.Write(fakeContent, 0, actual.Length);
    
            // assert
            Assert.Equal(fakeContent.Length, actual.Length);
    
            for (var i = 0; i < fakeContent.Length; i++)
            {
                Assert.Equal(fakeContent[i], actual[i]);
            }
    

    Since i am moking a mscorlib member and FileStream.Write is a instance call / not contains in the default set File, DateTime, FileInfo. I also added the following line during TestInitailization.

               Mock.Partial<FileStream>()
                  .For<byte[], int, int>((x, content, offset, len) => 
                x.Write(content, offset, len));
    

    [Disclaimer i work for telerik]

    Hope that helps,

    Mehfuz

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to extend a WPF application with database functionality. Which database engine would
I'm looking to extend the GridSplitter in some way to add a button which
I am creating an XML file which is based upon the Atom 1.0 specification.
I have a Maven (3.0.3) / GWT (2.4) project. I'm trying to write some
I have an automated build process that I'd like to extend so I can
I have a script and it has run into some problems: (function($) { $.fn.extend({
I want to test a class method that calls upon a parent method with
So, I have the following setup in Eclipse (Java): I have a project (lets
I'm trying to extend some base classes in Python: class xlist (list): def len(self):
I want to extend the basic ControlCollection in VB.NET so I can just add

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.