1

I want to unit test a class that reads the power information. I have a private readonly variable which i assign a value from System.Windows.SystemInformation.PowerStatus and i can use reflection to change this no problem but...

System.Windows.PowerStatus does not have a public constructor and won't let me inherit from it to derive a testPowerStatus class i can use with my tests.

I want to be able to override the getter for BatteryLifePercent and BatteryLifeRemaining property so i can test that the correct behavior happens when these are certain values.

Is there anyway i can do this? or is this impossible?

twigg
  • 146
  • 6

3 Answers3

2

You dont actually need to test a routine which assigns a value from System.Windows.SystemInformation.PowerStatus to a private variable, try to extract the logic you ned to test in a separate routine, for example a methode which has a number as a parameter which simulates the relivant information.

Take a look at the members the PowerStatus exposes, they are all either int, float or enums try to provide these as parameters to your units which will then be easier to test.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
1

The "right" way to do this is to create an abstraction layer. You should create a IPowerInfoProvider interface, and then create a stub implementing the interface for tests, and another implementation providing real power info.

Your BL should only know about the IPowerInfoProvider interface.

LMB
  • 1,137
  • 7
  • 23
  • I have created an interface then a class that implements that and returns the systeminformation and a class that returns mock information. Thank you – twigg Nov 13 '12 at 11:43
1

I would rather make an interface IPowerStatus having the properties and methods you need. Than I would create wrapper class for System.Windows.PowerStatus implementing IPowerStatus and recaling methods and getters/setters on keeped object. And for unit tests you can use some mock implementing your IPowerStatus.

mipe34
  • 5,596
  • 3
  • 26
  • 38