Mock.NonPublic.Arrange<int>(mockedClass, "_prop").Returns(5);
Works as long as my private variable is setup like so:
private int _prop {get; set;}
However these:
private int _prop;
or
private int _prop = 5;
get an error attempting to read it.
Thanks in advance,
Mark
MethodUnderTest(MyClass myClass)
{
myClass.MyMethod();
// Do some other stuff
myClass.MyMethod();
}
[TestMethod]
public void MyTest()
{
MyClass mockMyClass = Mock.Create<
MyClass
>(Behaviour.Strict);
mockMyClass.Arrange(x=>x.MyMethod());
// Arrange the other stuff
mockMyClass.Arrange(x=>x.MyMethod());
MethodUnderTest(mockMyClass);
mockMyClass.AssertAll();
}
[TestMethod]
public void MyTest()
{
MyClass mockMyClass = Mock.Create<
MyClass
>(Behaviour.Strict);
mockMyClass.Arrange(x=>x.MyMethod(), 2);
// Arrange the other stuff
MethodUnderTest(mockMyClass);
mockMyClass.AssertAll();
}
It's quite common that we will try to customise a Framework class but still reuse most of the logic from the base class. For example, we want to provide our own Navigation Provider inheriting from Microsoft.SharePoint.Navigation.SPNavigationProvider. And we want to override GetChildNodes class to add some addition site map node on top of the collection that base class return. So we will be calling base.GetChildNodes() from our GetChildNodes method.
Here is a simple example. The problem is I dont know how to reference the base class method in the Mock.Arrange method?
public
class
ParentClass
{
public
virtual
int
Sum()
{
return
0;
}
}
public
class
ChildClass : ParentClass
{
public
override
int
Sum()
{
return
1 +
base
.Sum();
}
}
[TestMethod]
public
void
RunChildClass()
{
var childClass =
new
ChildClass();
Mock.Arrange(() => childClass.Sum()).Returns(2);
// want to override ParentClass.Sum(), not ChildClass.Sum().
var result = childClass.Sum();
Assert.IsTrue(result == 3);
// instead I will get 2 here.
}