【发布时间】:2017-10-28 14:55:54
【问题描述】:
我环顾四周,找不到任何可以帮助我的地方。
所以我想单元测试的事件是我的 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);
}
}
如何修改我的单元测试以像上述步骤一样调用存储库/管理器?
抱歉,对这个很陌生。
【问题讨论】:
标签: c# unit-testing repository-pattern mvp