Unit Test View Model Button Press

Started by
0 comments, last by ChaosEngine 7 years, 6 months ago

Hello, I am new to unit testing and have the following question. I have a WPF app that uses MVVM and I am working on unit testing the view model. My UI has a button which the view model abstracts as a command, and when it is pressed it sets the state of a few properties and posts an event. My question is whether I should make one unit test that asserts everything I expect to happen from the button press (psuedocode):

// Arrange
 
btnCommand.Execute(null); // basically button press handler
 
Assert(StateA == X)
Assert(StateB == Y)
Assert(StateC == Z)
Verify event was posted

or should I separate these out into 4 separate tests with only one assert per test?

-----Quat
Advertisement

Let's say you have a UI that controls a traffic intersection.

One button changes the lights so that traffic flows north-south, the other east-west.

If you press the button for north-south, the following must happen.

- The north-south red lights are switched off

- The north-south green lights are switched on

- The east-west green lights are switched off

- The east-west red lights are switched on

What would happen if any of these actions/states did not happen? The system would be in an invalid state.

So to test one "unit" of functionality, your test should assert that everything under its control is in the correct state.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

This topic is closed to new replies.

Advertisement