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

  • Home
  • SEARCH
  • 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 194295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:34:58+00:00 2026-05-11T16:34:58+00:00

With reference to the Managed Extensibility Framework (MEF), I’m trying to work out how

  • 0

With reference to the Managed Extensibility Framework (MEF), I’m trying to work out how to create clean tests with mocks.

I have an exported component which has three private imports. Each imported object (field) needs to be mocked. Given that the CompositionContainer uses fancy reflection tactics to set imported private fields of composable parts, even in unit tests I will need to use the container to set those field values.

How do I tell the container at run time to accept a dynamic object I’ve created with Rhino Mocks as a valid export so that it can be used to satisfy the imports in the component I’m testing?

  • 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-11T16:34:59+00:00Added an answer on May 11, 2026 at 4:34 pm

    My question has been answered here.


    Hi Nathan

    There’s a couple of different options here.

    1. Using a batch, you can call the AddExportedObject method to add a mock instance to the container. AddExportedObject allows you to specify the contract for the instance that you are adding. i.e. batch.AddExportedObject(mockLogger, typeof(ILogger));

    2. You an also create a custom export provider to allow you add mock instances. If you follow this link, here are some utils that I use. http://pastie.org/467842. Within you’ll find a FakeExportProvider, along with FakeExportDefinitions. The FakeExportDefinitions take a func for the instance. This means you can pass it an instance, or even directly create a mock.

    Here’s sample code to illustrate usage.

        protected override void Context()
        {
            MockCache = MockRepository.GenerateStub<ICache>();
            var metadata = new Dictionary<string, object> ()
            var cacheDefinition = new FakeInstanceExportDefinition(typeof(ICache), MockCache, metadata);
            FakeProvider = new FakeExportProvider(f => ((FakeInstanceExportDefinition)f).Instance);
            FakeProvider.AddExportDefinitions(cacheDefinition);
            CacheExport = FakeProvider.GetExport<ICache>();
        }
    

    Now above I am querying the export provider directly. However, our container allows passing in an export provider in it’s construction. So you can do this…

    var container = new CompositionContainer(null, FakeProvider).

    HTH
    Glenn

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just a wild guess, could you try to define your… May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer You forgot your select; try this: var result = from… May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer Each program runs in at least one thread. You can… May 11, 2026 at 11:55 pm

Related Questions

With reference to the Managed Extensibility Framework (MEF), I'm trying to work out how
Some of the answers to a question I had about redirect_to made me think
how can one use a Safearray to pass an array of custom types (a
I'm stuck with .Net 1.1 application (i.e. I can not use the generics goodies
I'm wondering what are the recommended ways to handle situations where, in memory managed

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.