1

I Need to test the following code?

   public double LoadPercent
    {
        get { return _loadPercent; }
        private set
        {
            RaisePropertyChanging("LoadPercent");
            _loadPercent = value;
            RaisePropertyChanged("LoadPercent");
        }
    }

Im using xUnit framework and if the set was not private i would write the test like this:

 [Fact]
 public void LoadPercentTest()
    {
        // Arrange 
        var loader = new Loader();
        bool notify = false;

        // Act
        loader.PropertyChanged += (sender, args) =>
            {
                if (args.PropertyName == "LoadPercent")
                {
                    notify = true;
                }
            };

        loader.LoadPercent = 20;

        // Assert
        Assert.True(notify, "Notification Failed");
    }

I'm new to unit testing (beginner level), any help will be appreciated.

RayOldProf
  • 1,040
  • 4
  • 16
  • 40
  • 1
    Test the code of the class that sets the value of `LoadPercent` property. For example, if method `SetLoadPercent` changes value of `LoadPercent`, call it in your test. – Karel Frajták May 03 '13 at 11:02
  • Your problem has nothing to do with unit-testing, but has more to do with how to set a private property from outside of the class. See this: http://stackoverflow.com/a/1778410/261050 – Maarten May 03 '13 at 11:04
  • 3
    I would caution you against using reflection to call private methods within unit tests. If you have to break encapsulation to test the class properly, it indicates that your class design isn't testable. I agree with @KarelFrajtak : call the method(s) that, by extension, set the value of `LoadPercent`. – FMM May 03 '13 at 11:37

0 Answers0