Mock a Field

2 posts, 0 answers
  1. Colan
    Colan avatar
    7 posts
    Member since:
    Dec 2007

    Posted 26 Nov 2011 Link to this post

    Trying to mock a class provided by a vendor that contains a number of public fields.

    Either I'm not arranging the mock properly or JustMock does not support mocking fields. 

    Sample code

    // sample class to mock
    public class HasFields
    {
          public string FieldA;
    }

    // code to set up mokc
        var mock = Mock.Create<HasFields>();
        Mock.Arrange (() => mock.FieldA).Returns("it works");

    When executed the Mock.Arrange call throws the error:

    Telerik.JustMock.MockAssertionException: Lambda must contain a valid method to procceed

    In the test code if the field is changed to a property, the Mock.Arrange works. 

    Unfortunately changing the field to a property isn't an option with the classes provided by a vendor.
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 28 Nov 2011 Link to this post

    Hi Colan,

    Thank you for bringing up the question. Unfortunately, you can not mock a field since mocking happens by intercepting method body therefore you can mock properties (which has a get/ set method), events (add / remove method) and other methods.

    This is not something limited for JustMock but it is more or less general and the nature of .net IL.

    Kind Regards,
    Mehfuz
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft R3 2016 release webinar banner
Back to Top