public void LogPropertyTest() { var target = new PrivateAccessor(new ConcreteService()); var log = target.GetProperty("_log"); Assert.IsNotNull(log); Assert.AreEqual("NLog.Logger", ((NLog.Logger)log).ToString()); }
public void PrivateAccessor_ShouldGetSetProperty() { // ACT // Wrapping the instance holding the private property. var inst = new PrivateAccessor(new ClassWithNonPublicMembers()); // Setting the value of the private property. inst.SetProperty("Prop", 555); // ASSERT - Asserting with getting the value of the private property. Assert.AreEqual(555, inst.GetProperty("Prop")); }
public void ShouldGetArrangedPrivateProperty() { // ARRANGE // Create a mocked instance of your class under test with original behavior. // You can also use original instance object and perform partial mocking. var mockedClass = Mock.Create <ClassWithNonPublicMembers>(Behavior.CallOriginal); // Arranging: When the private Prop is called from the mockedClass instance, it should return 5. Mock.NonPublic.Arrange <int>(mockedClass, "Prop").Returns(5); // ACT // Wrapping the mocked instance with a private accessor. var inst = new PrivateAccessor(mockedClass); // Calling the non-public method by giving its exact name. var actual = inst.GetProperty("Prop"); // ASSERT - No matter the mock is wrapped, it should keep its arrangements. Assert.AreEqual(5, actual); }