mardi 31 mai 2016

What is the best way to test View Model that depends on child view models?

I have class MainViewModel that takes couple of child view models as dependency. I want to test MainViewModel methods. What is the best way to do it? How should I mock child view models?

My code looks like:

public class MainViewModel:ViewModelBase
{       
    public MainViewModel(ChildViewModel1 childViewModel1, ChildViewModel2 childViewModel2)
    {
        this.ChildViewModel1 = childViewModel1;
        this.ChildViewModel2 = childViewModel2;
    }

    public ChildViewModel1 ChildViewModel1 { get; set; }

    public ChildViewModel2 ChildViewModel2 { get; set; } 

    public void MethodThatIWantToTest()
    {
        this.ChildViewModel1.SomeProperty = "new value";
    }
}

public class ChildViewModel1:ViewModelBase
{
    private ISomeService someService;

    public ChildViewModel1(ISomeService someService)
    {
        this.someService = someService;
    }

    public string SomeProperty { get; set;}
}


//Test
public void Should_ChangeChildViewModel1SomeProperty_WhenCallingMethodThatIWantToTest()
{
    // arrange
    var mainViewModel = new MainViewModel(.., ..); // how to mock here??

    // act
    mainViewModel.MethodThatIWantToTest();

    // assert
    Assert.Equal(mainViewModel.ChildViewModel1.SomeProperty, "new value");
}       

Aucun commentaire:

Enregistrer un commentaire