我环顾四周,找不到任何可以帮助我的地方。
所以我希望单元测试的视图上的事件是我的OnFormLoadEvent。它看起来像这样:
public partial class SystemVariablesForm : Form, ISystemVariablesView {
private SystemVariablesPresenter presenter;
private readonly ISystemVariablesManager manager;
public SystemVariablesForm(ISystemVariablesManager _manager) {
manager = _manager;
InitializeComponent();
}
public float BindingLip {
get {
return (float)nudBindingLip.Value;
}
set => nudBindingLip.Value = (decimal)value;
}
public float HeadTrim {
get {
return (float)nudHeadTrim.Value;
}
set => nudHeadTrim.Value = (decimal)value;
}
public float FootTrim {
get {
return (float)nudFootTrim.Value;
}
set => nudFootTrim.Value = (decimal)value;
}
public string ErrorMessage {
get {
return lblErrors.Text;
}
set => lblErrors.Text = value;
}
public event EventHandler<EventArgs> SetSystemVariables;
public event EventHandler<EventArgs> OnFormLoad;
public event EventHandler<ErrorEventArgs> LogErrorToView;
public event EventHandler<EventArgs> SetImpositionFormAsActive;
private void SetSystemVariables_Load(object sender, EventArgs e) {
//Have to do this to avoid a dependency injection loop as the view relies on the presenter and the presenter relies on the view
presenter = new SystemVariablesPresenter(this, manager);
try {
OnFormLoad(this, e);
}
catch (Exception ex) {
LogErrorToView(this, new ErrorEventArgs(ex.Message));
}
}
}
然后通过此方法在我的演示者中获取:
private void DisplaySystemVariables(object sender, EventArgs e) {
try {
SystemVariables variables = _systemVariablesManager.ReturnSystemVariables();
_view.BindingLip = variables.BindingLip;
_view.HeadTrim = variables.HeadTrim;
_view.FootTrim = variables.FootTrim;
}
catch (Exception ex) {
LogErrorToView(this, new ErrorEventArgs(ex.Message));
}
}
这叫我的经理:
public class SystemVariablesManager : ISystemVariablesManager {
private ISystemVariablesRepository _systemVariablesRepo;
public SystemVariablesManager(ISystemVariablesRepository systemVariablesRepo) {
_systemVariablesRepo = systemVariablesRepo;
}
public Models.SystemVariables ReturnSystemVariables() {
return _systemVariablesRepo.ReturnSystemVariables();
}
public void SetSystemVariables(Models.SystemVariables systemVariables) {
_systemVariablesRepo.SetSystemVariables(systemVariables);
}
}
反过来调用我的存储库:
public Models.SystemVariables ReturnSystemVariables() {
if (File.Exists(expectedFilePath)) {
var json = JObject.Parse(File.ReadAllText(expectedFilePath))["SystemVariables"];
return JsonConvert.DeserializeObject<Models.SystemVariables>(json.ToString());
}
else {
throw new Exception("Setup file not located. Please run the Inital Set up application. Please ask Andrew for more information.");
}
}
现在我需要使用单元测试测试此事件,我选择了MOQ,但我不确定如何使用它来测试它。
到目前为止,我的单元测试看起来像这样:
[TestClass]
public class SystemVariablesPresenterTests {
[TestMethod]
private void OnFomLoad() {
var mockView = new Mock<ISystemVariablesView>();
mockView.Raise(r => r.OnFormLoad += null, new EventArgs());
Assert.IsNotNull(mockView.Object.HeadTrim);
}
}
如何像上述步骤一样修改单元测试以调用存储库/管理器?
对不起,对此很新。
答案 0 :(得分:0)
模拟的视图只是你的模拟。您还需要创建测试目标,该目标应该是演示者。我想有一种方法可以将模拟视图连接到真正的演示者。您还需要确定单位的边界,通常只是演示者。然后,您将需要模拟外部依赖项。由于边界只是演示者,因此管理者是需要模拟的外部依赖。基本上,您只是在测试时,当您的模拟视图触发事件时,您的测试目标(真正的演示者)正在对模拟的管理器做什么。存储库不参与此过程。
您还可以采取务实的方法,并确定该单位是演示者和经理组合在一起。然后,存储库成为外部依赖项,需要进行模拟。然后,您将测试模拟视图何时触发和事件,测试目标(真正的演示者+管理者)对模拟存储库执行的操作。