This is a migrated thread and some comments may be shown as answers.

Can we assign value to private variable using JustMost full version

1 Answer 82 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Rajendar
Top achievements
Rank 1
Rajendar asked on 24 Jun 2014, 09:23 AM
Hi Everyone,

From this link http://www.telerik.com/help/justmock/advanced-usage-private-accessor.html I understood that we can call private methods and properties but by using JustMock. Can I give some value to private variable in class
Example:
Class JustMock
{
       private string name="Testing"
       Private string ReturnSomeValue()
        {
            return name;
         }

}

Here my question is can I assign my own value to name variable with JustMock

Regards,
Rajendar.



1 Answer, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 26 Jun 2014, 02:02 PM
Hi Rajendar,

The answer is, yes you can. You can achieve this with the JustMock PrivateAccessor , like so:
[TestMethod]
public void TestMethod()
{
    // Arrange
    var test = new Foo();
    var privateAcc = new PrivateAccessor(test);
    privateAcc.SetField("name", "test");
 
    // Act
    var actual = privateAcc.CallMethod("ReturnSomeValue");
 
    // Assert
    Assert.AreEqual("test", actual);
}

I hope this helps.

Regards,
Kaloyan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
General Discussions
Asked by
Rajendar
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Share this question
or