2013-10-15 2 views
2

NotifyPropertyChanged 및 DispatchMethod로 장식 된 모델의 단위 테스트를 시도하고 있습니다.UnitTest 및 PostSharp 툴킷, 도메인 및 스레딩

BASEMODEL

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 
} 

DogModel

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.InvalidO을 perationException : DispatcherObjectAspect로 표시된 클래스의 인스턴스는 동기화 컨텍스트 (일반적으로 WPF 또는 Windows.Forms UI 스레드)가있는 스레드에서만 생성되거나 IDispatcherObject를 수동으로 구현해야합니다.

필요한 동기화 컨텍스트를 어떻게 제공 할 수 있습니까?

답변

1

분명히 내가 할하는 데 필요한 모든가 그것을 설정 ...

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

케이스 마감했다.