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

The Archive Base Latest Questions

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

For example take the following class: class GameBoard { public double Evaluate(PieceType cpusPieceType) {

  • 0

For example take the following class:

class GameBoard    
{
    public double Evaluate(PieceType cpusPieceType)
    {
        ...
        for (var i = 0; i < _tableRepository.Size; ++i)
            for (var j = 0; j < _tableRepository.Size; ++j)
                 if (_tableRepository [i, j] != PieceType.Empty)
                 {
                     var isMax = _tableRepository [i, j] == cpusPieceType;

                     var value = EvaluatePosition (i, j, _tableRepository [i, j]);
                     ...
                 }
        ...
    }

    protected virtual double EvaluatePosition (int row, int column, PieceType pieceType)
    { ... }
}

I would like to write a test for Evaluate function but this in turn depends on the values returned by EvaluatePosition function which is declared protected. Now, my idea was that I could use a class like:

class XGameBoard : GameBoard
{
    protected override double EvaluatePosition (int row, int column, PieceType pieceType)
    {
        // this will call EvaluatePosition_ShouldReturn which will be mocked and have a value previously set up
        return EvaluatePosition_ShouldReturn (row, column, pieceType);
    }
    public virtual double EvaluatePosition_ShouldReturn (int row, int column, PieceType pieceType)
    {
        return base.EvaluatePosition (row, column, pieceType);
    } 
}

The test would look like this:

_gameBoardMock = new Mock<XGameBoard>(...);

for (var i = 4; i < _size - 4; i += 2)
    for (var j = 4; j < _size - 4; j += 2)
        _gameBoardMock.Setup (x => x.EvaluatePosition_ShouldReturn (i, j, PieceType.Cross)).Returns (1.0);

var result = _gameBoardMock.Object.Evaluate (PieceType.Cross);

The problem is that EvaluatePosition inside the Evaluate function always returns 0, and not the overriden function is invoked which should return the value that was set up.

Thanks in advance.

  • 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-21T08:41:16+00:00Added an answer on May 21, 2026 at 8:41 am

    I would like to clarify the problem a little more. The situation is that the Evaluate function should return a double value based on some logic and the returned values of the EvaluatePosition function. Basically, the Evaluate function depends on the values returned by EvaluatePosition function, but the function has to be tested in isolation so I had to create a stub for the EvaluatePosition function and mimic the return values by setting up those initially. My approach seems to be usable the only thing that is needed is the following statement:

    _gameBoardMock.CallBase = true;
    

    Because the EvaluatePosition function is declated protected the values that it should return cannot be set up by Moq, therefore I created the XGameBoard class that declares the EvaluatePosition_ShouldReturn function which is public and usable by Moq and this way I can set up the values that the normal EvaluatePosition function should return:

    _gameBoardMock.Setup (x => x.EvaluatePosition_ShouldReturn (i, j, PieceType.Cross)).Returns (1.0);
    

    Because the EvaluatePosition function is overriden in the XGameBoard class, when this function is invoked it will invoke the EvaluatePosition_ShouldReturn function whose values are set up by Moq and should return the specified values.

    I hope that some may find this approach useful for unit testing. Happy coding.

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

Sidebar

Related Questions

Take the following example: class BookManager { ... }; class Book { public: void
Take for example following code from a class: public class Employee : IEntity {
So I have the following classes: class A{ public A(int n1){ n=n1; } int
I have a parent class called Exam and that has many instances of a
I'm trying to use C++ abstract base class in the way similar with Java
What is going to be the status of an object pointer when a new
I'm trying to correct all Checkstyle warnings for my project, but I keep stumbling
I am working on project that converts database entities into DTO objects. To do
I am using IIS to develop some web applications. I used to believe that
Question : What is the recommended way to specify an initial value for fields

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.