0

This might seem like a stupid question but I find myself facing this problem fairly often.

I want to write an simple application that monitors when a type on the keyboard and when I use the mouse so that I can monitor my computer usage and productivity.

Consider the following gherkin feature:

Feature: MouseInteractionMonitoring
    In order to know when the user is at the computer
    As an interaction monitor
    I want to be able to be able to monitor when the user moves the mouse

To my mind this does not seem testable.

So what should I do?

Should I resolve this using a layer of abstraction by writing a separate component that monitors mouse movement and then have that report to a user interaction module and mock the mouse movement component?

How does one deal with untestable code like the above?

I would really appreciate any advice you could offer on this.

K-Dawg
  • 3,013
  • 2
  • 34
  • 52

1 Answers1

1

The answer is usually to just think about the problem in a different way.

For example, I think the specific feature you have mentioned is actually quite testable as it is possible to move the mouse in code, and you should probably be able to fire click events, too. You'd have to get quite specific in the actual scenarios:

Scenario Outline: Should log mouse movements
Given the computer is <status>
When the mouse moves by more than two pixels
Then a mouse move user interaction is logged
  Examples:
  | status |
  | idle   |
  | active |

Scenario: should log left-mouse clicks
When the mouse is left-clicked
Then a left-click user interaction is logged

... etc.

whybird
  • 1,108
  • 8
  • 19
  • But how is it possible to simulate Mouse clicks in a testing environment? – K-Dawg Jun 11 '15 at 08:36
  • That doesn't help... I need to know how I would go about doing this in a testing environment... Maybe I need to broaden my code to have a mouse monitor, and a user interaction class...? The mouse monitor could then be mocked... Is that the right approach? – K-Dawg Jun 11 '15 at 08:43
  • I don't understand- why would that not work in a testing environment? Do you mean on a server whet your tests are really running under TeamCity or suchlike? If so, then it is possible it will work anyway (though you'd better open a window or something to stop the clicks actually doing anything!), but you're right that a better approach there may be to directly call all your mouse event handlers with many different variations for auto testing and separately do far fewer, possibly manual tests that prove real mouse events do actually get through to your handlers. It's a test pyramid approach. – whybird Jun 11 '15 at 08:58
  • No... what I mean is that you would need to simulate the mouse clicks for the test! You can't expect the user to start moving the mouse around and clicking during a test run... right? – K-Dawg Jun 11 '15 at 09:00
  • Also you don't want to be performing real mouse clicks and movements during the test with that class (in the other question). – K-Dawg Jun 11 '15 at 09:02
  • What I was trying to get to in my long comment agrees with what you just said - whether you *can* or not, you may not *want* to automate everything. At some point, you have to do real clicking and prove it gets through, but you probably want to minimise that. "Has to (or is selected to) be tested manually" (or less often) is not the same as "cannot be tested". – whybird Jun 11 '15 at 09:12
  • 1
    Oh I see what you mean! Thanks! – K-Dawg Jun 11 '15 at 09:15