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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:37:26+00:00 2026-05-23T04:37:26+00:00

When trying to unit-test a methode I run into a problem. The task of

  • 0

When trying to unit-test a methode I run into a problem. The task of the method is to move an node in an tree. This tree is build via the “Preorder Tree Traversal” method. http://blogs.sitepoint.com/hierarchical-data-database-2/ This methode uses Left and Right values of a node to position it in a tree.

In the methode the Lft and Rgt values will be changed for a node. In my testcase i’ll be moving the node “Meat” under “Banana”.

        var Food = new Node { Id = Guid.NewGuid(), Title = "Food", Ordinal = 0,Depth = 0, Lft = 1, Rgt = 18};
        var Fruit = new Node { Id = Guid.NewGuid(), Title = "Fruit", ParentNode = Food, Depth = 1, Ordinal = 0, Lft = 2, Rgt = 11 };
        var Red = new Node { Id = Guid.NewGuid(), Title = "Red", ParentNode = Fruit, Depth = 2, Ordinal = 0, Lft = 3, Rgt = 6 };
        var Cherry = new Node { Id = Guid.NewGuid(), Title = "Cherry", ParentNode = Red, Depth = 3, Ordinal = 0, Lft = 4, Rgt = 5 };
        var Yellow = new Node { Id = Guid.NewGuid(), Title = "Yellow", ParentNode = Fruit, Depth = 2, Ordinal = 1, Lft = 7, Rgt = 10 };
        var Banana = new Node { Id = Guid.NewGuid(), Title = "Banana", ParentNode = Yellow, Depth = 3, Ordinal = 0, Lft = 8, Rgt = 9 };
        var Meat = new Node { Id = Guid.NewGuid(), Title = "Meat", ParentNode = Food, Depth = 1, Ordinal = 1, Lft = 12, Rgt = 17 };
        var Beef = new Node { Id = Guid.NewGuid(), Title = "Beef", ParentNode = Meat, Depth = 2, Ordinal = 0, Lft = 13, Rgt = 14 };
        var Pork = new Node { Id = Guid.NewGuid(), Title = "Pork", ParentNode = Meat, Depth = 2, Ordinal = 1, Lft = 15, Rgt = 16 };

        var allNodes = new List<Node> {Food, Fruit, Red, Cherry, Yellow, Banana, Meat, Beef, Pork};

        var descendantsOfNodeFood = new List<Node>
                                  {
                                     Beef,
                                     Cherry,
                                     Red,
                                     Yellow,
                                     Pork,
                                     Banana,
                                     Meat,
                                     Fruit
                                  };

        var descendantsOfNodeFruit = new List<Node>
                                  {
                                     Red,
                                     Cherry,
                                     Banana,
                                     Yellow
                                  };

        var descendantsOfNodeRed = new List<Node>
                                  {
                                     Cherry
                                  };

        var descendantsOfNodeCherry = new List<Node> { };

        var descendantsOfNodeYellow = new List<Node>
                                  {
                                     Banana
                                  };

        var descendantsOfNodeBanana = new List<Node> { };

        var descendantsOfNodeMeat = new List<Node>
                                  {
                                     Beef,
                                     Pork
                                  };

        var descendantsOfNodeBeef = new List<Node> { };

        var descendantsOfNodePork = new List<Node> { };

        //Mock the node repository
        _mockNodeRepository.Setup(x => x.LoadNode(Food.Id)).Returns(Food);
        _mockNodeRepository.Setup(x => x.GetDescendants(Food.Id, It.IsAny<bool>())).Returns(descendantsOfNodeFood);
        _mockNodeRepository.Setup(x => x.GetDescendants(Fruit.Id, It.IsAny<bool>())).Returns(descendantsOfNodeFruit);
        _mockNodeRepository.Setup(x => x.GetDescendants(Red.Id, It.IsAny<bool>())).Returns(descendantsOfNodeRed);
        _mockNodeRepository.Setup(x => x.GetDescendants(Cherry.Id, It.IsAny<bool>())).Returns(descendantsOfNodeCherry);
        _mockNodeRepository.Setup(x => x.GetDescendants(Yellow.Id, It.IsAny<bool>())).Returns(descendantsOfNodeYellow);
        _mockNodeRepository.Setup(x => x.GetDescendants(Banana.Id, It.IsAny<bool>())).Returns(descendantsOfNodeBanana);
        _mockNodeRepository.Setup(x => x.GetDescendants(Meat.Id, It.IsAny<bool>())).Returns(descendantsOfNodeMeat);
        _mockNodeRepository.Setup(x => x.GetDescendants(Beef.Id, It.IsAny<bool>())).Returns(descendantsOfNodeBeef);
        _mockNodeRepository.Setup(x => x.GetDescendants(Pork.Id, It.IsAny<bool>())).Returns(descendantsOfNodePork);
        _mockNodeRepository.Setup(x => x.GetNodes()).Returns(allNodes);

When running the test all values are set to the correct ones, except the Lft and Rgt values of Meat. (They should be 9 – 10) I traced the problem back to the mock of the methode “GetDescendants(Meat.Id, It.IsAny()))”. This methode will be called twice in the method i try to test. The first time it should return a list with the nodes “Beef” and “Pork”. The second time, node “Beef” is placed under “Banana”, it should return a list with only the node “Pork”.

Who can help me figure out how the mock can return a list with 2 nodes the first time, and the list with only one node the second time?

I’m using “Microsoft.VisualStudio.TestTools.UnitTesting;” and “Moq;” for unit testing.

  • 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-23T04:37:27+00:00Added an answer on May 23, 2026 at 4:37 am
    returning different values on each invocation
    var mock = new Mock<IFoo>();
    var calls = 0;
    mock.Setup(foo => foo.GetCountThing())
        .Returns(() => calls)
        .Callback(() => calls++);
    // returns 0 on first invocation, 1 on the next, and so on
    Console.WriteLine(mock.Object.GetCountThing());
    

    from: http://code.google.com/p/moq/wiki/QuickStart

    However if your mock setup is that complex it may be better to use an in memory db or whatever you are using instead. Mocking logic is very fragile and hard to read.

    To answer your comment:

    var mock = new Mock<IFoo>();
    bool firstCall = true;
    mock.Setup(foo => foo.GetCountThing())
    .Returns(() => firstCall ? list1 : list2)
    .Callback(() => firstCall = false);
    

    should return the first list then the second one for all following calls.

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

Sidebar

Related Questions

I am trying to write a unit test for an action method which calls
Following on from this question...I'm trying to unit test the following scenario: I have
I am trying to write a unit test for a method that uses a
I'm trying to setup a unit test but whenever I run phpunit -c app
I am trying to write a unit test for a method which takes an
I am trying to write a unit test for the following method in my
I'm trying to Unit Test a class that has many internal functions. These obviously
I'm trying to unit test a page in my ASP.NET MVC application that, when
I am running on Castle's trunk, and trying to unit-test a controller-action where validation
I'm trying to implement a unit test for a function in a project that

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.