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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:08:20+00:00 2026-06-02T03:08:20+00:00

This is my interface public interface IWork { string GetIdentifierForItem(Information information); } and my

  • 0

This is my interface

public interface IWork
{
    string GetIdentifierForItem(Information information);
}

and my class

public class A : IWork
{
[ImportMany]
public IEnumerable<Lazy<IWindowType, IWindowTypeInfo>> WindowTypes { get; set; }

public string GetIdentifierForItem(Information information)
{
    string identifier = null;
    string name = information.TargetName;

    // Iterating through the Windowtypes 
    // searching the 'Name' and then return its ID  
    foreach (var windowType in WindowTypes)
    {
        if (name == windowType.Metadata.Name)
        {
            identifier = windowType.Metadata.UniqueID;
            break;
        }
    }
    return identifier;
}
}

Problem : I want to unit test the method GetIdentifierForItem

Here is what I tried doing to solve it –

(1)Create a mock Lazy and set the values that it needs to return on property gets

var windowMock = new Mock<Lazy<IWindowType, IWindowTypeInfo>>();
windowMock.Setup(foo => foo.Metadata.Name).Returns("Data");
windowMock.Setup(foo => foo.Metadata.UniqueID).Returns("someString");

(2)Create a window type list and the above mocked object and then set it to the created A object

var WindowTypesList = new List<IWindowType, IWindowTypeInfo>>();
WindowTypesList.Add(windowMock.Object);
A a = new A();
a.WindowTypes = WindowTypesList;

(3) Create the information mock

var InfoMock = new Mock<Information>();
InfoMock.Setup(foo => foo.TargetName).Returns("Data");

To put all of the above together as the unit test

[TestMethod]
public void GetIDTest()
{
    var windowMock = new Mock<Lazy<IWindowType, IWindowTypeInfo>>();
    windowMock.Setup(foo => foo.Metadata.Name).Returns("Data");
    windowMock.Setup(foo => foo.Metadata.UniqueID).Returns("someString");

    var WindowTypesList = new List<Lazy<IWindowType, IWindowTypeInfo>>();
    WindowTypesList.Add(windowMock.Object);

    A a = new A();
    a.WindowTypes = WindowTypesList;
    var InfoMock = new Mock<Information>();
    InfoMock.Setup(foo => foo.TargetName).Returns("Data");

    string expected = "someString"; // TODO: Initialize to an appropriate value
    string actual;
    actual = a.GetIdentifierForItem(InfoMock.Object);
    Assert.AreEqual(expected, actual);

}

This unit test fails to execute and throws an exception ‘TargetInvocationException’ and veiwing the detail, it looks like I am doing something that I should not be doing.

But I am not sure how do it other way. I have read some of the links in the Quickstart guide of Moq. I know I am missing something. Can you help me by guiding how to unit test this?

  • 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-02T03:08:21+00:00Added an answer on June 2, 2026 at 3:08 am

    You don’t need to Mock the Lazy<T,TMetadta>. It is flexible enough to work with your test. Instead, Mock the IWindowTypeInfo

    [TestMethod]
    public void GetIDTest()
    {
        var windowTypeInfoMock = new Mock<IWindowTypeInfo>();
        windowTypeInfoMock.Setup(foo => foo.Name).Returns("Data");
        windowTypeInfoMock.Setup(foo => foo.UniqueID).Returns("someString");
        var lazyWindow =
             new Lazy<IWindowType, IWindowTypeInfo>(windowTypeInfoMock.Object);
    
        var WindowTypesList = new List<Lazy<IWindowType, IWindowTypeInfo>>();
        WindowTypesList.Add(lazyWindow);
    
        var a = new A();
        a.WindowTypes = WindowTypesList;
        var InfoMock = new Mock<Information>();
        InfoMock.Setup(foo => foo.TargetName).Returns("Data");
    
        string expected = "someString";
        string actual;
        actual = a.GetIdentifierForItem(InfoMock.Object);
        Assert.AreEqual(expected, actual);
    }
    

    Your test passes on my machine with only small modifications, you do not need to use a composition container for this test.

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

Sidebar

Related Questions

Say I've got this interface: public interface IFoo { string Text {get;} } And
I'm looking at this: public interface IAjaxCallbackEventHandler : ICallbackEventHandler { string CallbackResponse { get;
I have an interface and a class implementing the interface like this: public interface
I have a proxied method in a MongoRepository extender class like this: public interface
This code is invalid: interface Foo { public void foo(final String string); } public
I have this interface: public interface Command<T> { T execute(String... args); } it works
Given this interface public interface IMyInterface { string Method1(); } Why is this valid
I have this interface public interface TestInterface { [returntype] MethodHere(); } public class test1
I have an interface with: IList<int> CategoryIDs {get;set;} Then a class that inherits this
I have an interface class public interface AsyncTaskExecuteCommand { public Object executeCommand(String jsonLocation) throws

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.