Reputation: 3066
I am trying to do a very simple thing: Set an initial value to a property, call a method which callback should modify the property value, and read the property at the end.
Class Test2 is using ITest1 to automatize some actions.
public interface ITest1
{
decimal Value { get; set; }
void Increment();
}
public class Test2
{
ITest1 test1;
public Test2(Itest1 test1)
{
this.test1 = test1;
}
public void Increment()
{
this.test1.Increment();
}
public Get()
{
return this.test1.Value;
}
}
In order to achieve this behaviour I setted up this test:
[TestClass]
public class Test2Test
{
private Test2 test2;
private decimal value;
[TestInitialize]
public void Setup()
{
var test1Mock = new Mock<ITest1>();
test1Mock.SetupGet(m => m.Value).Returns(value);
test1Mock
.Setup(m => m.Increment())
.Callback(() => value++);
this.test2= new Test2(test1Mock.Object);
}
[TestMethod]
public void Get_Returns0()
{
Assert.AreEqual(0, this.test2.Get());
}
[TestMethod]
public void Get_AfterIncrement_Returns1()
{
this.test2.Increment();
Assert.AreEqual(1, this.test2.Get());
}
}
The second test is returning always 0. Why is this happening?
Upvotes: 1
Views: 481
Reputation: 3066
The solution is to return actions instead of variables as:
.Return(() => value)
instead of .Return(value)
Answer found here.
Upvotes: 1