방금 Jason Dolinger's video on MVVM을 보았습니다. 그리고 올바르게 설정하고 단위 뷰 테스트 모델의 ICommand 속성을 테스트하는 방법에 대한 설명이 필요합니다.뷰 모델에서 메서드 또는 명령을 단위 테스트해야합니까?
FooBarCommand ICommandProperty를 사용하여 다음 ViewModel 클래스를 고려하십시오.
public class ViewModel : IViewModel
{
public ICommand FooBarCommand { get; private set; }
public bool CanExectuteFooBar()
{
return true;
}
public void FooBar()
{
//Do some FooBarish stuff
}
}
public interface IViewModel
{
void FooBar();
System.Windows.Input.ICommand FooBarCommand { get; }
}
public class FooBarCommand : ICommand
{
private ViewModel vm;
public FooBarCommand(ViewModel vm)
{
this.vm = vm;
}
public bool CanExecute(object parameter)
{
return vm.CanExectuteFooBar();
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
vm.FooBar();
}
}
그래서 testVM.FooBar()를 호출하거나 testVM.FooBarCommand.Execute를 호출하여 명령을 실행하여 하나 내가 FOOBAR를 실행할 수의 ViewModel의 FOOBAR 기능을() 테스트 단위 해요 경우(). 어느 쪽이 좋을까요? 궁극적으로 뷰의 단추가 FooBarCommand 속성에 바인딩되어 FooBarCommand 속성을 테스트하지 않고 있습니다.
또한 뷰가 ViewModel이 아닌 IViewModel에 바인딩되므로 IViewModel 인터페이스의 FooBar() 메서드를 완전히 수정해야합니다.
는) – blindmeis
아 그래 내가 예를했을 때 나는 그것에 대해 생각하지 않았습니다 알고있다. 나는 지금 그 문제에서 그것을 고쳤다. –
FooBar 메서드를 인터페이스에서 제거하는 것이 옳습니다. 메서드와 CanExecute 메서드는 실제로 내부적 일 수 있습니다. 메소드 대 테스트를 시작하면 클래스의 private 구현과 공용 "인터페이스"를 테스트하는 것과 다르지 않습니다. 뷰 모델의 소비자는 명령에서만 작동하기 때문입니다. – Rich