Unit testing broken? #237
Answered
by
miroiu
jg-at-home
asked this question in
Q&A
-
|
I'm trying to use nodify in a project and want to Unit Test my ViewModel (that's one of the points of MVVM, yes?!) But when I try to create an instance in my test project, it crashes due to a null reference in this: Is there a work-around? (I could subclass my VM again to override the dispatcher but that makes me uncomfortable and might not even work). |
Beta Was this translation helpful? Give feedback.
Answered by
miroiu
Aug 7, 2025
Replies: 1 comment 1 reply
-
|
You can remove the public class ObservableObject : INotifyPropertyChanged
{
/// <summary>
/// Gets or sets the dispatcher to use to dispatch PropertyChanged events. Defaults to UI thread.
/// </summary>
public virtual Action<Action> PropertyChangedDispatcher { get; set; }
/// <summary>
/// Occurs when a property value changes
/// </summary>
public event PropertyChangedEventHandler? PropertyChanged;
public ObservableObject()
{
if (Application.Current != null)
{
PropertyChangedDispatcher = Application.Current.Dispatcher.Invoke;
}
else
{
PropertyChangedDispatcher = (Action action) => action();
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
jg-at-home
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can remove the
PropertyChangedDispatcherand call thePropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName))directly. Or try something like this: