我有一个RelayCommand
我正在尝试测试。 RelayCommand
包含Service Call
来验证我的用户。如下所示:
private MvxCommand _signIn;
public MvxCommand SignIn
{
get
{
return _signIn ?? (_signIn = new MvxCommand(() =>
{
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
EndpointAddress endpoint = new EndpointAddress(AppResources.AuthService);
var client = new MyService(binding, endpoint);
client.AuthorizeCompleted += ((sender, args) =>
{
try
{
if (args.Result)
{
//Success.. Carry on
}
}
catch (Exception ex)
{
//AccessDenied Exception thrown by service
if (ex.InnerException != null && string.Equals(ex.InnerException.Message, "Access is denied.", StringComparison.CurrentCultureIgnoreCase))
{
//Display Message.. Incorrect Credentials
}
else
{
//Other error... Service down?
}
}
});
client.AuthorizeAsync(UserName, Password, null);
}));
}
}
但现在我正在使用NUnit
来测试我的ViewModels
,而我对如何测试RelayCommand
我能做到:
[Test]
public void PerformTest()
{
ViewModel.SignIn.Execute();
}
但是这不返回SignIn
方法是否成功的信息。
如何测试包含RelayCommand
的{{1}}?
答案 0 :(得分:1)
所以最后我使用Dependency Injection
将一个服务注入到我的viewmodel的构造函数中,如下所示:
public IMyService client { get; set; }
public MyClass(IMyService myservice)
{
client = myservice
}
然后我可以重构我的SignIn
方法:
private MvxCommand _signIn;
public MvxCommand SignIn
{
get
{
return _signIn ?? (_signIn = new MvxCommand(() =>
{
client.AuthorizeCompleted += ((sender, args) =>
{
try
{
if (args.Result)
{
//Success.. Carry on
}
}
catch (Exception ex)
{
//AccessDenied Exception thrown by service
if (ex.InnerException != null && string.Equals(ex.InnerException.Message, "Access is denied.", StringComparison.CurrentCultureIgnoreCase))
{
//Display Message.. Incorrect Credentials
}
else
{
//Other error... Service down?
}
}
});
client.AuthorizeAsync(UserName, Password, null);
}));
}
}
使用Assign-Act-Assert设计模式使用模拟服务测试我的ViewModel
:
[Test]
public void PerformTest()
{
List<object> objs = new List<object>();
Exception ex = new Exception("Access is denied.");
objs.Add(true);
AuthorizeCompletedEventArgs incorrectPasswordArgs = new AuthorizeCompletedEventArgs(null, ex, false, null);
AuthorizeCompletedEventArgs correctPasswordArgs = new AuthorizeCompletedEventArgs(objs.ToArray(), null, false, null);
Moq.Mock<IMyService> client = new Mock<IMyService>();
client .Setup(t => t.AuthorizeAsync(It.Is<string>((s) => s == "correct"), It.Is<string>((s) => s == "correct"))).Callback(() =>
{
client.Raise(t => t.AuthorizeCompleted += null, correctPasswordArgs);
});
client.Setup(t => t.AuthorizeAsync(It.IsAny<string>(), It.IsAny<string>())).Callback(() =>
{
client.Raise(t => t.AuthorizeCompleted += null, incorrectPasswordArgs);
});
var ViewModel = new MyClass(client.Object);
ViewModel.UserName = "correct";
ViewModel.Password = "correct";
ViewModel.SignIn.Execute();
}