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

  • Home
  • SEARCH
  • 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 3423308
In Process

The Archive Base Latest Questions

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

I have these classes: public static class UnitOfWorkSS { public static IUnitOfWork Begin() {

  • 0

I have these classes:

public static class UnitOfWorkSS 
{
  public static IUnitOfWork Begin()
  {
    return IoC.Resolve<IUnitOfWork>();
  }
}

public class PostService
{
  using (IUnitOfWork unitOfWork = UnitOfWorkSS.Begin())
  {
    //don't forget to sanitize html content
    htmlContent = _htmlSanitizer.Sanitize(htmlContent);

    IPost post = _factory.CreatePost(byUser, title, htmlContent);                    

    _postRepository.Add(post);

    unitOfWork.Commit();
  }
}

How can I mock the classes UnitOfWorkSS and unitOfWork?

  • 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-18T06:20:21+00:00Added an answer on May 18, 2026 at 6:20 am

    It looks like the only thing you are doing with the call to Begin() is returning your configured class for that particular interface: IUnitOfWork

    You really just need to make sure that your call to Begin() returns a mock implementation of IUnitOfWork

    One of two ways you can do this:

    Option One – Refactor UnitOfWorkSS so that you can set the instance of IUnitOfWork to be returned

    public static class UnitOfWorkSS  
    {
        private static IUnitOfWork _unitOfWork;
        public static IUnitOfWork UnitOfWork
        {
            set { _unitOfWork = value; }
            private get{ _unitOfWork ?? (_unitOfWork = IoC.Resolve<IUnitOfWork>()); }
        }
    
        public static IUnitOfWork Begin()  
        {  
            return UnitOfWork;
        }  
    }  
    
    [TestMethod]
    public void DoStuff()
    {
        var mockUnitOfWork = new Mock<IUnitOfWork>();
        UnitOfWorkSS.UnitOfWork = mockUnitOfWork.Object;
    
        //Do some setup and verify
    }
    

    Option Two – Simply register a mock instance of IUnitOfWork with your IoC Container

    private Mock<IUnitOfWork> _mockUnitOfWork;
    
    [TestInitialize]
    public void Init()
    {
        _mockUnitOfWork = new Mock<IUnitOfWork>();
    
        //Making a lot of assumptions about your IoC here...
        IoC.Register<IUnitOfWork>(_mockUnitOfWork.Object);
    }
    
    [TestMethod]
    public void DoStuff()
    {
        _mockUnitOfWork.Setup( ... );
    
        //Do some verification
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have these two classes: public class Base { public static int
All -- I have these two methods in one of my classes: public static
Concerning data binding I have these classes: public class Foo : List<Bar> { public
I have these two classes public class Person { } public class Company {
I have these classes in C# (.NET Framework 3.5) described below: public class Base
I have XNA game and it contains these classes public partial class Bird :
I currently have (among others) these classes: public class Main extends Activity { Panel
suppose we have these classes: public class Record { int key; int value; Record(){
in c++ i have these below classes class A { public: int __thiscall check(char
Is there a way to force classes in Java to have public static final

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.