1

I am using nj4x and it has a class that has readonly fields as follows.

   public class MarketInformation
    {
    public readonly double ASK;
    public readonly double BID;
    public readonly double DIGITS;
    public readonly double EXPIRATION;
    public readonly double FREEZELEVEL;
    }

I am writing unit tests for classes and methods writing these classes. For isolation/ mocking I am using Nsubstitute and MicrosoftFakes (shims mainly). I want to override these fields so when they are called in my methods I get predictable results for testing. I generated shim for this class but all it provides me is a constructor, now allowing me to initialize it still. Is there any way to set these fields from outside this class?

6
  • I'm not very knowledgeable with mocks/fakes etc. but isn't the idea that you set your hard-coded predictable values for your fields inside the mock class, i.e. you don't need to pass it in or assign it from outside. A mock is supposed to be an implementation of the abstraction that you're mocking, just like the "real" class is in an implementation.
    – rory.ap
    Dec 30, 2016 at 13:40
  • @rory.ap implementing mocks is a seperate idea however, I am using frameworks for mocking and isolation. Implementing mocks require abstract/interface classes. however those are not available here. Dec 30, 2016 at 13:45
  • 1
    You need to create an interface which you can mock. The members need to be virtual. (NSubstitute can only work with virtual members of the class, so any non-virtual code in the class will actually execute!) reference
    – NtFreX
    Dec 30, 2016 at 13:51
  • I am using nj4x library. Cannot create interfaces. Dec 30, 2016 at 13:54
  • Then I would recommend to switch to a mocking library which supports non virutal members (reference). Why do you eaven need to mock fields? This shouldn't be a use case..
    – NtFreX
    Dec 30, 2016 at 13:55

2 Answers 2

3

It isn't pretty, but if you have access to the instance that needs to be modified, you can use the SetField method of the PrivateObject class. The PrivateObject class is part of the MS unit testing framework.

For example, consider the class:

public class ClassWithReadOnly
{
    public readonly string Foo;

    public ClassWithReadOnly()
    {
        Foo = "bar";
    }
}

You can set the read-only field like so:

var test = new ClassWithReadOnly();
Console.WriteLine("Before Foo == '{0}'", test.Foo);
var po = new PrivateObject(test);
po.SetField("Foo", "oof");
Console.WriteLine("After Foo == '{0}'", test.Foo);

The output will be:

Before Foo == 'bar'

After Foo == 'oof'

0
1

You'll probably need to create a wrapper class with your own get properties to stub or shim it out. You could create your own interface if you wanted to use stubs (not shown).

public class MarketInformationWrapper : MarketInformation
{
    ...
    public double Ask
    {
        get { return ASK; }
    }
    ...
}
1
  • I doubt it won't do me any good. This will work only when I can pass the object into class or method under test. What I want is something what shims do. Like I create a shim for class dummy, say shimDummy. then I can just say shimDummy.AllInstances.Ask = (Dummy) => 5; this way i will get 5 whenever it is invoked. Dec 30, 2016 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.