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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:20:40+00:00 2026-05-18T20:20:40+00:00

I am using Moq (which I am very new too as well as TDD

  • 0

I am using Moq (which I am very new too as well as TDD period). And I wanted to Moq up an instance of my main Winforms form so I could test a few methods on there. Is this possible? Its constructor takes an object of Assembly().

I was trying the following attempts unsuccessful:

var mockMainForm = new Mock<MainForm>();
mockMainForm.Setup(x => x.Assembler).Returns(new Assembly());
return mockMainForm.Object;

But I can’t access any properties or methods on this object once it is returned. Is this possible to do?

But I get errors that ( failed: System.ArgumentException : Expression is not a method invocation: x => x.Assembler
at Moq.ExpressionExtensions.ToMethodCall(LambdaExpression expression))

  • 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-18T20:20:40+00:00Added an answer on May 18, 2026 at 8:20 pm

    Mocking using Moq will only mock the interfaces and virtual methods of a class. Assembler property needs to be defined as virtual.

    In any case Mocking a Windows Form is not a good mocking – it has a big bag of WIN32 stuff which make your tests brittle. If you need to unit test and mock a form, create an interface that your form needs to implement and mock that for objects that need to interact with your form.

    public interface IHasAssembler
    {
        Foo Assembler {get; set;}
    }
    
    public class MyForm : IHasAssembler
    {
         //
    }
    

    then mock the interface and not the form:

    Mock<IHasAssembler> myMock = new Mock<IHasAssembler>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Moq (which I am very new too as well as TDD
I'm brand new to Moq (using v 4) and am struggling a little with
I'm using NUnit and Moq to test a class which uses a generic WCF
I want to mock Find method which expects a predicate using Moq: public PurchaseOrder
I am using Moq framework for Mocking. I have a SocialWorker which derives from
Using Moq for generation of Stubs and Mocks in my unit tests, I have
i have just started using Moq ver (3.1) and i have read blogs and
We are using Moq to unit test our service classes, but are stuck on
I want to mock only the GetValue method of the following class, using Moq:
I am using the Moq framework for unit testing and would like to be

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.