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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:03:11+00:00 2026-05-13T08:03:11+00:00

The class I want to test is my ArticleManager class, specifically the LoadArticle method:

  • 0

The class I want to test is my ArticleManager class, specifically the LoadArticle method:

public class ArticleManager : IArticleManager
{
      private IArticle _article;

      public ArticleManger(IDBFactory dbFactory)
      {
            _dbFactory = dbFactory;
      }

      public void LoadArticle(string title)
      {
            _article = _dbFactory.GetArticleDAO().GetByTitle(title);

      }
}

My ArticleDAO looks like:

public class ArticleDAO : GenericNHibernateDAO<IArticle, int>, IArticleDAO
{
       public virtual Article GetByTitle(string title)
       {
           return Session.CreateCriteria(typeof(Article))
               .Add(Expression.Eq("Title", title))
               .UniqueResult<Article>();
       }
}

My test code using NUnit and Moq:

[SetUp]
public void SetUp()
{
        _mockDbFactory = new Mock<IDBFactory>();
        _mockArticleDao = new Mock<ArticleDAO>();

        _mockDbFactory.Setup(x => x.GetArticleDAO()).Returns(_mockArticleDao.Object);

        _articleManager = new ArticleManager(_mockDbFactory.Object);
}


[Test]
public void load_article_by_title()
{
     var article1 = new Mock<IArticle>();

     _mockArticleDao.Setup(x => x.GetByTitle(It.IsAny<string>())).Returns(article1.Object);

     _articleManager.LoadArticle("some title");

     Assert.IsNotNull(_articleManager.Article);
}

The unit test is failing, the object _articleManager.Article is returning NULL.

Have I done everything correctly?

This is one of my first unit tests so I am probably missing something obvious?

One issue I had, was that I wanted to mock IArticleDao but since the class ArticleDao also inherits from the abstract class, if I just mocked IArticleDao then the methods in GenericNHibernateDao are not available?

  • 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-13T08:03:11+00:00Added an answer on May 13, 2026 at 8:03 am

    Preface: I’m not familiar with using Moq (Rhino Mocks user here) so I may miss a few tricks.

    I’m struggling to follow some of the code here; as Mark Seemann pointed out I don’t see why this would even compile in its current state. Can you double check the code, please?

    One thing that sticks out is that you’re injecting a mock of IDBFactory into Article manager. You then make a chained call of:

    _article = _dbFactory.GetArticleDAO().GetByTitle(title)
    

    You’ve not provided an implementation of GetArticleDAO. You’ve only mocked the LoadByTitle bit that happens after the GetARticleDAO call. The combination of mocks and chained calls in a test are usually a sign that the test is about to get painful.

    Law of Demeter

    Salient point here: Respect the Law of Demeter. ArticleManager uses the IArticleDAO returned by IDBFactory. Unless IDBFactory does something really important, you should inject IArticleDAO into ArticleManager.

    Misko eloquently explains why Digging Into Collaborators is a bad idea. It means you have an extra finicky step to set up and also makes the API more confusing.

    Furthermore, why do you store the returned article in the ArticleManager as a field? Could you just return it instead?

    If it’s possible to make these changes, it will simplify the code and make testing 10x easier.

    Your code would become:

    public class ArticleManager : IArticleManager
    {
          private IArticleDAO _articleDAO
    
          public ArticleManger(IArticleDAO articleDAO)
          {
                _articleDAO = articleDAO;
          }
    
          public IArticle LoadArticle(string title)
          {
                return _articleDAO.GetByTitle(title);
          } 
    }
    

    You would then have a simpler API and it’d be much easier to test, as the nesting has gone.

    Making testing easier when relying on persistence

    In situations where I’m unit testing code that interacts with persistence mechanisms, I usually use the repository pattern and create hand-rolled, fake, in-memory repositories to help with testing. They’re usually simple to write too — it’s just a wrapper around a dictionary that implements the IArticleRepository interface.

    Using this kind of technique allows your ArticleManager to use a fake persistence mechanism that behaves very similarly to a db for the purpose of testing. You can then easily fill the repository with data that helps you test the ArticleManager in a painless fashion.

    Mocking frameworks are really good tools, but they’re not always a good fit for setting up and verifying complicated or coherent interactions; if you need to mock/stub multiple things (particularly nested things!) in one test, it’s often a sign that the test is over-specified or that a hand-rolled test double would be a better bet.

    Testing is hard

    … and in my opinion, doubly hard if you start with mocking frameworks. I’ve seen a lot of people tie themselves in knots with mocking frameworks due to the ‘magic’ that happens under the hood. As a result, I generally advocate staying away from them until you’re comfortable with hand-rolled stubs/mocks/fakes/spies etc.

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

Sidebar

Ask A Question

Stats

  • Questions 404k
  • Answers 404k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can generate elements like this: <xsl:element name="notes"> <!-- inject… May 15, 2026 at 5:34 am
  • Editorial Team
    Editorial Team added an answer This is my personal favorite: Dive into HTML5 This one… May 15, 2026 at 5:34 am
  • Editorial Team
    Editorial Team added an answer Without any more details I can only guess that artifacts… May 15, 2026 at 5:34 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.