Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在XUnitTest中处理Yes No DisplayAlert?_C#_Xamarin_Xamarin.forms_Xunit - Fatal编程技术网

C# 如何在XUnitTest中处理Yes No DisplayAlert?

C# 如何在XUnitTest中处理Yes No DisplayAlert?,c#,xamarin,xamarin.forms,xunit,C#,Xamarin,Xamarin.forms,Xunit,请帮助我如何在XUnitTest中使用Wait DisplayAlert LoginPageViewModel代码: public ICommand LogoutCommand; public LoginPageViewModel() { LogoutCommand= new RelayCommand(LogoutExecution); } async void LogoutExecution() { result = await App.Current.MainPage.Dis

请帮助我如何在XUnitTest中使用Wait DisplayAlert

LoginPageViewModel代码:

public ICommand LogoutCommand;

public LoginPageViewModel()
{
   LogoutCommand= new RelayCommand(LogoutExecution);
}

async void LogoutExecution()
{
   result = await App.Current.MainPage.DisplayAlert("Alert!","Are you sure you want to logout?", "Yes", "No");
   if (result)
   {
      //Code Execution
      await LogOut();
   }
}
XUnitTest代码:

public class LoginPageViewModelTest
{
        LoginViewModel loginvm;
        public LoginViewModelTest()
        {
            Xamarin.Forms.Mocks.MockForms.Init();            
            var app = new Mock<App>().Object;
            app.MainPage = new Mock<Xamarin.Forms.ContentPage>().Object;
            loginvm = new LoginPageViewModel();
        }        

        [Fact]
        public void LogoutCommandExecuted()
        {
             loginvm.LogoutCommand.Execute(null);
        }
 }
公共类LoginPageViewModelTest
{
LoginView模型loginvm;
公共登录视图模型测试()
{
Xamarin.Forms.Mocks.MockForms.Init();
var app=new Mock().Object;
app.MainPage=新建Mock().Object;
loginvm=新的LoginPageViewModel();
}        
[事实]
public void LogoutCommandExecuted()
{
loginvm.LogoutCommand.Execute(null);
}
}
当我测试LogoutCommandExecuted时,点击这一行后执行未完成。“等待App.Current.MainPage.DisplayAlert”


请帮助我,如何在命令执行方法中执行if“App.Current.MainPage.DisplayAlert”?

从设计角度来看,视图模型在调用
DisplayAlert
时与UI问题紧密耦合

这些关注点应该被抽象出来,以允许更好的灵活性、可测试性和可维护性

利用依赖倒置原则

例如,创建一个抽象来表示所需的功能

public interface IDialogService {
    Task<bool> DisplayAlert (String title, String message, String accept, String cancel);

    //...other members
}
假设没有其他紧密耦合的依赖项,那么
LoginPageViewModel
应该能够单独进行测试,而不会产生与UI问题相关的任何连锁反应。如果正确模拟的依赖项被注入测试对象中


在生产环境中,可以将系统配置为使用依赖服务将服务实现注入依赖类。

从设计角度来看,视图模型在发出
DisplayAlert
调用时与UI问题紧密耦合

这些关注点应该被抽象出来,以允许更好的灵活性、可测试性和可维护性

利用依赖倒置原则

例如,创建一个抽象来表示所需的功能

public interface IDialogService {
    Task<bool> DisplayAlert (String title, String message, String accept, String cancel);

    //...other members
}
假设没有其他紧密耦合的依赖项,那么
LoginPageViewModel
应该能够单独进行测试,而不会产生与UI问题相关的任何连锁反应。如果正确模拟的依赖项被注入测试对象中


在生产环境中,可以将系统配置为使用依赖服务将服务实现注入依赖类。

该UI/实现关注点应该从viewmodel中抽象出来,以允许更大的灵活性。该UI/实现关注点应该从viewmodel中抽象出来,以允许更大的灵活性灵活性。谢谢@Nkosi,但在“wait dialog.DisplayAlert”之后没有代码执行相同问题谢谢@Nkosi,但在“wait dialog.DisplayAlert”之后没有代码执行相同问题
public class LoginPageViewModel : ViewModelBase {
    private readonly IDialogService dialog;

    public LoginPageViewModel(IDialogService dialog) {
       LogoutCommand = new RelayCommand(LogoutExecution);
    }

    public ICommand LogoutCommand { get; private set; }

    void LogoutExecution() {
       logOut += onLogOut;
       logOut(this, EventArgs.Empty);
    }

    private event EventHdnalder logOut = delegate { };

    private async void onLogOut(object sender, EventArgs args) {
        logOut -= onLogOut;
        var result = await dialog.DisplayAlert("Alert!","Are you sure you want to logout?", "Yes", "No");
        if (result) {
          //Code Execution
          await LogOut();
       }
    }

    //...
}