Reputation: 915
In the below statements, I'm wanting to set the Navigation
object's IsNew
flag to false
and then also, return false
to a method IsCurrentStepUnlocked
invoked on that object
However, on line 4 when i retrieve the value of IsNew
, it is 'true'
I don't seem to get both, set the value on the object and also setup something to return a predetermined value.
1. var mockStatementInformation = Mock.Of<IStatementInformation>();
2. mockStatementInformation.Navigation = new Navigation { IsNew = false };
3. Mock.Get(mockStatementInformation).Setup(x => x.Navigation.IsCurrentStepUnlocked()).Returns(false);
4. var isNew = mockStatementInformation.Navigation.IsNew;
Navigation, is defined simply as shown below.
public class Navigation
{
public Navigation()
{
IsNew = true;
}
public bool IsNew { get; set; }
}
How do i set a nested property or call methods on it using Moq?
Upvotes: 1
Views: 6183
Reputation: 247561
Reference: Moq Quickstart - linq-to-mocks
And assuming this example
public interface IStatementInformation {
Navigation Navigation { get; set; }
}
public class Navigation {
public Navigation() {
IsNew = true;
}
public virtual bool IsNew { get; set; }
public virtual bool IsCurrentStepUnlocked() {
return true;
}
}
Where both method and property return true by default.
When tested like this results are as expected
var mockStatementInformation = Mock.Of<IStatementInformation>(m =>
m.Navigation == new Navigation { IsNew = false } &&
m.Navigation.IsCurrentStepUnlocked() == false
);
Assert.IsFalse(mockStatementInformation.Navigation.IsCurrentStepUnlocked());
Assert.IsFalse(mockStatementInformation.Navigation.IsNew);
Upvotes: 1