c# - UnitTest 和 PostSharp 工具包、域和线程

标签 c# nunit postsharp

我正在尝试对用 NotifyPropertyChanged 和 DispatchMethod 装饰的模型进行单元测试。

基础模型

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

public abstract class BaseModel : INotifyPropertyChanged, IDisposable
{
    #region Fields

    private readonly HashSet<string> ignorablePropertyNameses = new HashSet<string>();

    private bool isDirty;

    #endregion

    #region Constructors and Destructors

    protected BaseModel() { this._propertyChanged += this.OnAnyPropertyChanged; }

    #endregion

    #region Public Events

    public event PropertyChangedEventHandler PropertyChanged
    {
        add { this._propertyChanged += value; }
        remove { this._propertyChanged -= value; }
    }

    #endregion

    #region Events

    private event PropertyChangedEventHandler _propertyChanged;

    #endregion

    #region Public Properties

    public HashSet<string> IgnorablePropertyNames
    {
        get { return this.ignorablePropertyNameses; }
    }

    public bool IsDirty
    {
        get { return this.isDirty; }
    }

    #endregion

    #region Public Methods and Operators

    public void Dispose() { this._propertyChanged -= this.OnAnyPropertyChanged; }

    public void RaisePropertyChanged(string propertyName)
    {
        if (null != this._propertyChanged)
        {
            this._propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public virtual void Save()
    {
        this.isDirty = false;
        MessageBox.Show("Changes have been saved");
    }

    public void SetClean() { this.isDirty = false; }

    #endregion

    #region Methods

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this._propertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private void OnAnyPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
    {
        if (!this.ignorablePropertyNameses.Contains(propertyChangedEventArgs.PropertyName))
        {
            this.isDirty = true;
        }
    }

    #endregion
}

狗模型

using System;
using System.Threading;

using AGP.WinForms.MVC.PassiveView;

using PostSharp.Toolkit.Domain;
using PostSharp.Toolkit.Threading;

[NotifyPropertyChanged]
public class DogModel : BaseModel
{
    #region Fields

    private Timer timer;

    #endregion

    #region Constructors and Destructors

    public DogModel()
    {
        this.IgnorablePropertyNames.Add("CurrentDateAndTime");
        this.timer = new Timer(state => this.BroadcastTime(), null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
    }

    #endregion

    #region Public Properties

    public string Breed { get; set; }

    public DateTime CurrentDateAndTime { get; set; }
    public string Name { get; set; }

    #endregion

    #region Methods

    [DispatchedMethod]
    private void BroadcastTime() { this.CurrentDateAndTime = DateTime.Now; }

    #endregion
}

使用此测试:

using NUnit.Framework;

[TestFixture]
public class DogModelTests
{
    [Test]
    public void DirtyFlag()
    {
        var model = new DogModel();
        Assert.IsFalse(model.IsDirty);
    }

}

但在测试执行时出现以下错误:

System.InvalidOperationException:用 DispatcherObjectAspect 标记的类的实例只能在具有同步上下文的线程(通常是 WPF 或 Windows.Forms UI 线程)上创建,或者必须手动实现 IDispatcherObject。

我如何提供所需的同步上下文?

最佳答案

显然我需要做的就是设置它......

    [SetUp]
    public void SetUp()
    {
        SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
    }

案件结案。

关于c# - UnitTest 和 PostSharp 工具包、域和线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19387289/

相关文章:

c# - NUnit 测试用例不是从继承类运行的

c# - Common.Logging 使用 PostSharp 和 NLog 2.0

c# - 使用 Postsharp 将方面应用到程序集的风险

c# - 在 Xamarin.Forms 中使用 Thread.Sleep

c# - 读取逗号分隔文件并将数据放入 ListView - C#

c# - 保存文件对话框并导出到 Excel 工作表

c# - 为什么在使用 TestCaseSource 时 nUnit 测试会被忽略?

C# 委托(delegate)为对象

postsharp - 在 Postsharp 编译期间获取项目路径/输出路径

c# - 部分崩溃的应用程序?如何捕获无法捕获的异常?