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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:42:37+00:00 2026-05-25T16:42:37+00:00

I’ve heard somewhere that mocking an entity is a BAD thing, and you should

  • 0

I’ve heard somewhere that mocking an entity is a BAD thing, and you should only mock services (even if we’re not doing full-blown DDD, but “sorta” DDD).

So, given the following typical story, how do you write a test for it?

  • If a customer has a Preferred status, she should be notified when Stuff happens.

Preferred status is something dependent on other things, like, the customer has recently purchased 10 Widgets, or his name starts with “A”, etc. Anyway, let’s suppose we have already implemented this functionality. How do I write a test for the above story? Specifically, I’m interested in how testability requirement would affect my design in this case.

  1. I can use some advanced mock framework (like Isolator) which lets me mock non-virtual properties. No correlation between testability and design, no problem.
  2. I can make the IsPreferred property virtual and mock it (stub actually). Don’t know why, but that feels dirty. See the question at the top of the post. Also, not sure how it improves my design.
    2a. Hide the entity behind the ICustomer interface and mock it. Totally uncool.
  3. I can make it a read-write property, but that would be a very bad design decision.

How do you handle such stories?

  • 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-25T16:42:38+00:00Added an answer on May 25, 2026 at 4:42 pm

    I would’t put too much logic into entities. They should care about their own state, eg. care about consistency of their own fields. But they shouldn’t care about consistency within the whole system. Your example of “user.AddOrder” that is setting a user state is going much too far IMHO.

    You also loose reusability of your entities if they do everything. If you get additional requirements (eg. additional situations like import of data, new kind of order, new kind of rule), your entity model gets in the way, it is not flexible enough.

    This lack of flexibility shows up in the unit tests. When you isolate classes in the unit tests, it gets clear what’s actually encapsulated and can’t be separated anymore. If you get a problem with that, your encapsulation is most probably not very useful. So unit tests are a great proof of reusability.

    Example (pseudocode, don’t mind if I miss the point of your application, it’s just to show where logic belongs to):

    class User
    {
        AccountState State { get; }
        void MakeTopSeller()
    }
    
    class Order
    {
        decimal GetTotal();
        User Salesman { get; }
    }
    
    
    class OrderService
    {
      void AddOrder()
      {
          // ....
          if (order.GetTotal() > ToSellerAmount)
          {
              order.Salesman.MakeTopSeller();
          }
      }
    }
    

    Edit: If you still think that your approach is appropriate (I mean I can’t decide from what I know about it), you don’t need to change it. But if your entity takes another entity to set a state, and you need that state in the test, you need to do all this in the test: create an order and add it to the user.

    There is another way to split things off. You could put the rule into a separate class, eg. using the strategy pattern. It is usually hard to set strategies on entities, because the underlying database layer needs to inject them.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am doing a simple coin flipping experiment for class that involves flipping a
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm trying to create an if statement in PHP that prevents a single post

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.