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

Mock a Field

1 Answer 77 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Colan
Top achievements
Rank 1
Colan asked on 27 Nov 2011, 05:16 AM
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.

1 Answer, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 28 Nov 2011, 10:46 AM
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 >>

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