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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:59:21+00:00 2026-06-13T13:59:21+00:00

I have made a generic databuilder for unit test, because I got tired of

  • 0

I have made a generic databuilder for unit test, because I got tired of implementing constructors and Build methods again and again. Does this obscure the beauty of the pattern (that I really DO love)?

Here is my generic builder (built in C# with Moq):

public class GenericMockBuilder<T> where T: class
{
    private readonly Mock<T> _objectMock = new Mock<T>(); 

    protected Mock<T> ObjectMock
    {
        get { return _objectMock; }
    }

    public T Build()
    {
        return _objectMock.Object; 
    }
}

And here is an example interface and its concrete builder that inherrits the generic one:

public interface IFoo
{
    int Bar();
}

public class FooBuilder: GenericMockBuilder<IFoo>
{
    public FooBuilder WithBar(int barValue)
    {
        base.ObjectMock.Setup(x => x.Bar()).Returns(barValue);
        return this;
    }

}

The builder will construct a foo mock like this:

        IFoo fooMock = new FooBuilder().WithBar(12).Build();

I would love to hear oppinions and suggestions for improvement.

EDIT (Eventraising example added):

public class FooBuilderWithEvent: GenericMockBuilder<IFoo>
{

    public FooBuilderWithEvent RaiseEvent(FooEventArgs fooEventArgs)
    {
        base.ObjectMock.Raise(m => m.FooEvent += null, fooEventArgs); 
        return this; 
    }

}

Building Mock and raising event would look like this:

        FooBuilderWithEvent fooBuilderWithEvent = new FooBuilderWithEvent(); 
        IFoo fooMock = fooBuilderWithEvent.Build();

        //Create testobject and prepare for event here

        fooBuilderWithEvent.RaiseEvent(new FooEventArgs()); 
  • 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-13T13:59:22+00:00Added an answer on June 13, 2026 at 1:59 pm

    pros

    • Nice domain-specific fluent interface

    cons

    • Another interface, which nobody familiar with (me, and I think many other developers do not see anything complex on mock setup with standard fluent interface of Moq)
    • Creating builder class for each mocked interface (yes, you can create builders only for complex mocks, but using of two different mocking interfaces could confuse developers)
    • Creating setup method for each mocked member

    Don’t know why you are asking for Moq sample, because you already have all this code inside builder:

    var mock = new Mock<IFoo>();
    mock.Setup(foo => foo.Bar()).Returns(42);
    

    If lambdas and mock.Object calls are confusing to you, try RhinoMocks:

    Exect.Call(foo.Bar()).Return(42);
    

    Also I have a question – how are you going to raise events with only mocked object instance?

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

Sidebar

Related Questions

I have made a generic Observer interface and an Observable class, but can't compile
I have made a generic handler (.ashx) to fetch some database values using jquery
I have made a generic PHP script to use for an AJAX call. I
I have downloaded the android 2.2.3_r1 and set up build environement. I have made
I have made a generic parser for parsing ascii files. When I want to
I am trying to implement Koch's Snowflake. I have made a generic list, for
Hello im trying to build a generic repository on top of linq2sql. I have
I have made a generic function to show jQuery dialogs in my app. this
Okay the question title may not have made sense... mostly because I don't know
I have made a jQuery toggle for a menu that I had in mind.

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.