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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:54:25+00:00 2026-05-15T14:54:25+00:00

I have a class A which has the following: public class A { [Import(typeof(IMyService)]

  • 0

I have a class A which has the following:

public class A {
    [Import(typeof(IMyService)]
    public IMyService MyService { get; set; }

    public A() {
        CompositionInitializer.SatisfyImports(this);
    }

    public void DoWork() {
        //Blah
        MyService.DoIt();
        //Blah
    }
}

And a Test to test this (seperate Dll – obviously)

[TestMethod]
public void TestDoWork() {
    //Blah
    DoWork();
    //Assert assert
}

This fails as attempting to call ‘MyService’ gives me null.
I’ve then tried:

[ClassInitialize]
public void InitialiseClass() {
    var myService = new Mock<IMyService>();
    MyService = myService.Object;
}

with ‘MyService’ declared as:

[Export(typeof(IMyService))]
public IMyService MyService { get; set; }

But still no joy, am I missing something – is this even possible?

I’m using SL3, MEF Preview 9 and MOQ.

Any help appreciated!

Cheers

Chris

  • 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-15T14:54:26+00:00Added an answer on May 15, 2026 at 2:54 pm

    Your class should look like this:

    public class A 
    {
        private readonly IMyService _myService;
    
        [ImportingConstructor]
        public A(IMyService myService)
        {
            _myService = myService;
        }
    
        public void DoWork() {
            //Blah
            _myService.DoIt();
            //Blah
        }
    }
    

    And your test should look like this:

    [TestMethod]
    public void DoWork_invokes_IMyService_DoIt() 
    {
        // arrange mock and system under test
        var myService = new Mock<IMyService>();
        var a = new A(myService.Object);
    
        // act    
        a.DoWork();
    
        // assert that DoIt() was invoked
        myService.Verify(x => x.DoIt());
    }
    

    The fact that you use MEF should not be important in unit tests. MEF only comes into play when wiring many components together, which is exactly the opposite of what happens in a unit test. A unit test is by definition a test of a component in isolation.

    Edit: if you prefer property injection, then your class doesn’t need a constructor and the arrange part in your unit test should look like this instead:

        var myService = new Mock<IMyService>();
        var a = new A();
        a.MyService = myService.Object;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a public class which has the following method and instance variable: public
I have a UserBll class which has following method public string GetaspnetUserIdByUserName(string name )
I have a DirectoryMonitor class which works on another thread. It has the following
I have a class which has implemented INotifyPropertyChanged . This class UserInfo has a
I have following code in Java: import java.util.*; public class longest{ public static void
I have a class which has a number of overloaded methods: public class CustomerCommandHandlers
I have the following class which implements 3 JPanels. 1 Panel has a label,
I have the following class, which as you will see has rather a rather
I have a struts2 action class which looks something like this: //import relevant packages
I have a class which has a function to retrieve children elements from a

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.