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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:56:19+00:00 2026-06-04T16:56:19+00:00

Assume we have an Order class with a method called Approve . When this

  • 0

Assume we have an Order class with a method called Approve. When this method is called, it checks certain conditions and either puts the Order in the state of Approved or throws an exception. In the service layer, we’ve got something like this:

var order = _repository.Single(o => o.ID == orderID);
order.Approve();
_context.SaveChanges(); // or _session.SaveChanges(); 

There are 2 ways to test this method and I’d like to hear your insight on this:

Solution 1: Stub the repository to return an Order object. Then assert the Order is in the state of “Approved”.

Solution 2: Stub the repository to return a Mock Order object. Assert that Approve() method was called.

Solution 1 is easier and I personally favor state-based testing to interaction-based testing, as the latter can target implementation details and should be avoided. However, I believe testing that the given Order is in the state of Approved is not the concern of this service method. I think we need a separate test method for the Order class to test whether an exception is thrown or the Order’s state is changed to Approved.

Solution 2 may sound logical as we are delegating the responsibility of Approving an Order to the Order class itself. So perhaps we need 2 tests for this service method: One to ensure it delegates the task of Approving an Order to the Order class and one to ensure it saves the changes.

What’s your insight on this? Which solution do you prefer?

Cheers

  • 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-04T16:56:20+00:00Added an answer on June 4, 2026 at 4:56 pm

    Unit tests are to test whether the observed behavior is conforming to expectations/specification.

    The answer to your question boils down to what you consider “expected behavior” in this case: a) if the expected behavior is that the Order is in approved state after calling the service method, then test the state; b) if the expected behavior is that the approve action is delegated, then test the method call.

    You will need to test the Order object’s behavior as well (so that calling Approve() changes the state to approved) in either case.

    The second solution plays well as it decouples the behavior of the two objects, but if there are more than one ways that the order can be in approved state (and that’s what you are testing — case a)), then you limit the accepted behavior needlessly.

    Also, I would create a separate test for testing the saving part, if that is not essential to the approval part

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

Sidebar

Related Questions

Assume I have a C# class like this: [XmlRoot(floors)] public class FloorCollection { [XmlElement(floor)]
Assume I have this domain object... public class SpansMultipleTables { public int CommonID {get;
Assume you have the following simple objects: class Order { public Customer[] Customers {
Suppose I have a class 'Application'. In order to be initialised it takes certain
assume I have a such html order <div id=header>>Header(logo, navigation menus etc)will be here,
Lets assume I have the following 3 entities: Customer,Order,Product which interact in the View
Assume we have a single Windows form with a button called SimpleButton1. The following
let's assume that I have some codes like this. What should I write there
I have extended the ActiveRecord::Base class as follows: lib/activerecord_ext.rb: class ActiveRecord::Base named_scope( :recent, :conditions
I have a class method (implemented in a shared object in UNIX environment) which

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.