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.Setu开发者_JAVA百科p(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.
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.
精彩评论