의 호출 비동기 방법은 내가 단위 테스트에 MOQNullReferenceException이 조롱 객체
를 사용하여 다음 뷰 모델의 LoginExecute 방법을 시도하고 때 INavigationService 내 테스트의 모습이public interface INavigationService
{
Frame Frame { get; set; }
void Navigate(Type type);
void Navigate(Type type, object parameter);
Task TestMethod();
void GoBack();
}
과 같은
public class LoginViewModel : ViewModelBase, ILoginViewModel
{
INavigationService navigationService;
IDialogService dialogService;
IAdminService adminService;
public RelayCommand LoginCommand { get; set; }
private string _productID;
public string ProductID
{
get { return _productID; }
set
{
_productID = value;
RaisePropertyChanged("ProductID");
}
}
public LoginViewModel(INavigationService navigationService, IDialogService dialogService, IAdminService adminService)
{
this.navigationService = navigationService;
this.dialogService = dialogService;
this.adminService = adminService;
InitializeCommands();
}
private void InitializeCommands()
{
LoginCommand = new RelayCommand(() => LoginExecute());
}
public async Task LoginExecute()
{
await this.navigationService.TestMethod();
this.navigationService.Navigate(typeof(ITherapistsViewModel));
}
public void Initialize(object parameter)
{
}
}
이
[TestMethod()]
public async Task LoginCommandTest()
{
var navigationService = new Mock<INavigationService>();
var dialogService = new Mock<IDialogService>();
var adminService = new Mock<IAdminService>();
LoginViewModel loginVM = new LoginViewModel(navigationService.Object, dialogService.Object, adminService.Object);
await loginVM.LoginExecute();
//Asserts will be here
}
문제는 라인
await this.navigationService.TestMethod();
라고하며 NullReferenceException이 발생합니다. "await"없이 같은 메서드가 호출되면 예상대로 작동합니다. 메서드가 일반 NavigationService 구현 (모의가 아닌)에서 호출되는 경우에도 정상적으로 작동합니다. 비동기 메서드 호출이 NullReferenceException을 생성하는 이유를 이해해 주시겠습니까?
이 정확한 문제는 오늘 아침 내 머리카락을 몇 시간 동안 계속 내 보냈습니다. –