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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:55:51+00:00 2026-05-11T07:55:51+00:00

I’m trying to unit test a method that performs a fairly complex operation, but

  • 0

I’m trying to unit test a method that performs a fairly complex operation, but I’ve been able to break that operation down into a number of steps on mockable interfaces like so:

public class Foo {       public Foo(IDependency1 dp1, IDependency2 dp2, IDependency3 dp3, IDependency4 dp4)     {         ...     }      public IEnumerable<int> Frobnicate(IInput input)     {         var step1 = _dependency1.DoSomeWork(input);         var step2 = _dependency2.DoAdditionalWork(step1);         var step3 = _dependency3.DoEvenMoreWork(step2);         return _dependency4.DoFinalWork(step3);     }      private IDependency1 _dependency1;     private IDependency2 _dependency2;     private IDependency3 _dependency3;     private IDependency4 _dependency4; } 

I’m using a mocking framework (Rhino.Mocks) to generate mocks for purposes of testing, and structuring the code in the fashion shown here has been very effective thus far. But how do I unit test this method without having one big test that needs every mock object and every expectation set every time? For example:

[Test] public void FrobnicateDoesSomeWorkAndAdditionalWorkAndEvenMoreWorkAndFinalWorkAndReturnsResult() {     var fakeInput = ...;     var step1 = ...;     var step2 = ...;     var step3 = ...;     var fakeOutput = ...;      MockRepository mocks = new MockRepository();      var mockDependency1 = mocks.CreateMock<IDependency1>();     Expect.Call(mockDependency1.DoSomeWork(fakeInput)).Return(step1);      var mockDependency2 = mocks.CreateMock<IDependency2>();     Expect.Call(mockDependency2.DoAdditionalWork(step1)).Return(step2);      var mockDependency3 = mocks.CreateMock<IDependency3>();     Expect.Call(mockDependency3.DoEvenMoreWork(step2)).Return(step3);      var mockDependency4 = mocks.CreateMock<IDependency4>();     Expect.Call(mockDependency4.DoFinalWork(step3)).Return(fakeOutput);      mocks.ReplayAll();      Foo foo = new Foo(mockDependency1, mockDependency2, mockDependency3, mockDependency4);     Assert.AreSame(fakeOutput, foo.Frobnicate(fakeInput));      mocks.VerifyAll(); } 

This seems incredibly brittle. Any change to the implementation of Frobnicate causes this test to fail (like breaking down step 3 into 2 sub-steps). It’s an all-in-one sort of thing, so trying to use multiple smaller tests isn’t going to work. It starts to approach write-only code for future maintainers, myself included next month when I’ve forgotten how it works. There has to be a better way! Right?

  • 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. 2026-05-11T07:55:52+00:00Added an answer on May 11, 2026 at 7:55 am

    Test each implementation of IDependencyX in isolation. Then you will know that each individual step of that process is correct. When testing them individually, test every possible input and special condition.

    Then do an integration test of Foo, using the real implementations of IDependencyX. Then you will know that all the individual parts are plugged together correctly. It’s often enough to just test with one input, because you are only testing simple glue code.

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

Sidebar

Ask A Question

Stats

  • Questions 141k
  • Answers 141k
  • 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 From memory I think this will do it: ObjectFactory.WhatDoIHave May 12, 2026 at 8:06 am
  • Editorial Team
    Editorial Team added an answer It's bad form to return arrays if not needed, and… May 12, 2026 at 8:05 am
  • Editorial Team
    Editorial Team added an answer select * from employees where employeeID in (select memberID from… May 12, 2026 at 8:05 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.