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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:21:15+00:00 2026-05-16T14:21:15+00:00

I am interested in the best way to write unit tests for a class

  • 0

I am interested in the best way to write unit tests for a class whose public API involves some kind of a flow, for example:

public class PaginatedWriter {
public void AppendLine(string line) { ... }
public IEnumerable<string> GetPages() { ... }
public int LinesPerPage { get; private set; }
}

This class paginates text lines into the given number of lines per page. In order to test this class, we may have something like:

public void AppendLine_EmptyLine_AddsEmptyLine() { ... }
public void AppendLine_NonemptyLine_AddsLine() { ... }
public void GetPages_ReturnsPages() {
 writer.AppendLine("abc");
 writer.AppendLine("def");
 var output = writer.GetPages();
 ...
}

Now, my question is: is it OK to make calls to AppendLine() in the last test method, even though we are testing the GetPages() method?

I know one solution in such situations is to make AppendLine() virtual and override it but the problem is that AppendLine() manipulates internal state which I don’t think should be the business of the unit test.

  • 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-16T14:21:16+00:00Added an answer on May 16, 2026 at 2:21 pm

    The way I see it is that tests usually follow a pattern like ‘setup – operate – check – teardown’.

    I concentrate most of the common setup and teardown in the respective functions.

    But for test specific setup and teardown it is part of the test method.

    I see nothing wrong with preparing the state of the Object Under Test using method calls of that object. In OOP I would not try to decouple the state from the operations since the paradigm goes to great lengths to unify them and if possible even hide the state. In my view the unit under test is the Class – state and methods.

    I do make visual distinction in the code by separating the setup block from the operate block and the verify block with an empty line.

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

Sidebar

Related Questions

So I'm interested in hearing different thoughts about what is the best way to
I'm interested in what you find are the best development references for learning and
I'm interested in learning some (ideally) database agnostic ways of selecting the n th
I'd be interested in some before-and-after c# examples, some non-idiomatic vs idiomatic examples. Non-c#
Is it in best interests of the software development industry for one framework, browser
Interested if anyone has used VSTS Database Edition extensively and, if so, which features
I'm interested in seeing a good diff algorithm, possibly in Javascript, for rendering a
I am interested in moving a number of my projects from Visual Studio and
Although I'm specifically interested in web application information, I would also be somewhat curious
I'm interested in doing comparisons between the date string and the MySQL timestamp. However,

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.