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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:03:39+00:00 2026-06-12T10:03:39+00:00

I have a unit test to verify that an object (say Foo ) will

  • 0

I have a unit test to verify that an object (say Foo) will call certain method (say Bar) when an event is fired with certain eventArgs. To mock the said method, I use virtual and stub the Foo class

Mock<Foo> stubbedFoo = new Mock<Foo>(mockEventProvider);

mockEventProvider.Raise( x => x.MyEvent += null, myEventArgs ) //fire the event
stubbedFoo.Verify(foo => foo.Bar()); verify Bar is called as a result

However, the above failed, Bar will not be called, apparently because the Foo object is not event constructed. However if I add a line like below:

Mock<Foo> stubbedFoo = new Mock<Foo>(mockEventProvider);
var workAround = stubbedFoo.Object //adding this workaround will work
mockEventProvider.Raise( x => x.MyEvent += null, myEventArgs ) //fire the event
stubbedFoo.Verify(foo => foo.Bar()); verify Bar is called as a result

It will work, because calling get on .Object apparently forces mock to construct the object. Is there a more elegant solution than adding this line ?

  • 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-12T10:03:41+00:00Added an answer on June 12, 2026 at 10:03 am

    I don’t think you can. I checked out the moq source and poked through it and it doesn’t look like the proxy intercepter from castle is actually created until you call .Object. Look at this trace:

    public object Object
    {
        get { return this.GetObject(); }
    }
    
    private object GetObject()
    {
        var value = this.OnGetObject();
        this.isInitialized = true;
        return value;
    }
    

    Followed by

    protected override object OnGetObject()
    {
        if (this.instance == null)
        {
            this.InitializeInstance();
        }
    
        return this.instance;
    }
    

    Which does this:

    private void InitializeInstance()
    {
        PexProtector.Invoke(() =>
        {
            this.instance = proxyFactory.CreateProxy<T>(
                this.Interceptor,
                this.ImplementedInterfaces.ToArray(),
                this.constructorArguments);
        });
    }
    

    ProxyFactory actually creates the object and wraps it in a proxy

    public T CreateProxy<T>(ICallInterceptor interceptor, Type[] interfaces, object[] arguments)
    {
        var mockType = typeof(T);
    
        if (mockType.IsInterface)
        {
            return (T)generator.CreateInterfaceProxyWithoutTarget(mockType, interfaces, new Interceptor(interceptor));
        }
    
        try
        {
            return (T)generator.CreateClassProxy(mockType, interfaces, new ProxyGenerationOptions(), arguments, new Interceptor(interceptor));
        }
        catch (TypeLoadException e)
        {
            throw new ArgumentException(Resources.InvalidMockClass, e);
        }
        catch (MissingMethodException e)
        {
            throw new ArgumentException(Resources.ConstructorNotFound, e);
        }
    }
    
    • 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
In my unit test how can I verify that an event is raised by
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
I have a unit test where I run a method for every item in
I have a unit test that tests if an Exception is throw, but this
I have a unit test project in Visual Studio 2010 (.NET 4) that utilizes
I need to create a unit test for a method that returns an xmldocument.
I have various methods that I would like to unit test using Visual Studio's
I have a unit tests that test if the first name of a baby

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.