-2

I am trying to unit test my View Model in a WPF MVVM application. This application currently runs correctly when run

My ViewModel implements INotifyPropertyChanged and raises the correct PropertyChanged events When unit testing, the PropertyChanged event is null and therefore throws exceptions

I have gone down a rabbit hole about the Application.Current, but have confirmed it is set while running the unit tests

What do I need to set/mock to prevent this from happening?

Zyth21
  • 39
  • 6
  • _"What do I need to set/mock to prevent this from happening?"_ -- obviously, you need something to subscribe to the `PropertyChanged` event. See duplicate for one approach to this. Feel free to post a new question if you run into trouble, and you are willing to put the effort in to include a proper [mcve], along with a detailed explanation of what the code does, how that's different from what you want, and what _specifically_ you need help with. – Peter Duniho Mar 15 '21 at 22:35

1 Answers1

0

The invocation of PropertyChanged should always check for a null value.

e.g.

public void RaisePropertyChanged(string propertyName = null)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Peregrine
  • 4,287
  • 3
  • 17
  • 34
  • 1
    While I agree with this and it does solve my initial problem, it does not allow me to test if the PropertyChanged events are called? – Zyth21 Mar 15 '21 at 22:22