-1

I want to set the default values to variables and list while testing my code using Nunit.

My code

public class Myclass
    {
        static bool isShown = false;
        protected static List<string> registery = new List<string>();
        //My code
    }

I need to change the default value. Because every test case this value will be changed.

I am using NUnit to test my application. I tried some thing by referring this. But it is not working.

[TestFixture]
    public class MyclassTest
    {
        Myclass obj = new Myclass();

        [SetUp]
        public void Init()
        {
            typeof(FusionLicenseProvider).GetField("isShown ", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance).SetValue(obj, false);
            typeof(FusionLicenseProvider).GetField("registery", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance).SetValue(obj, null);
        }

        [TearDown]
        public void Cleanup()
        {
            typeof(FusionLicenseProvider).GetField("isShown ", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance).SetValue(obj, false);
            typeof(FusionLicenseProvider).GetField("registery", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance).SetValue(obj, null);
        }

        [Test]
        public void Test01()
        {
          // Test case
        }
   }

Could you any one please help me to get rid from this?

Arunkumar
  • 73
  • 1
  • 11
  • 1
    What does "not working" mean? Exceptions? Unexpected results? Compiler-errors? Apart from this you don´t need the `GetField`-bindingflag to set the value of a field via reflection. But as your both fields are `static`, you should use `Bindingflags.Static` instead of `Instance`. – MakePeaceGreatAgain Jun 18 '18 at 14:23
  • @HimBromBeere Thank you! It works when I change the `Instance` to `Static`. But How can I remove old data from `List`. – Arunkumar Jun 19 '18 at 04:28
  • I just initialized the `List` Now its working. Thank you @HimBromBeere – Arunkumar Jun 19 '18 at 04:47
  • 1
    To delete old data just set the `register`-fields value to `new List()` instead of `null` in your `Cleanup`-code. – MakePeaceGreatAgain Jun 19 '18 at 10:16

2 Answers2

1

I bet you get some NullReferenceException, because the requested field was not found. This is caused by your BindingFlags. In order set a private static field you´ll need this:

typeof(FusionLicenseProvider).GetField("isShown ", BindingFlags.NonPublic | BindingFlags.Static).SetValue(obj, false);

To avoid such errors in the future you should definitly add some null-checks before:

var field = typeof(FusionLicenseProvider).GetField("isShown ", BindingFlags.NonPublic | BindingFlags.Static);
if(field != null)
    field.SetValue(obj, false);
else
    throw new Exception(...);
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • Its working when I am changing the `Instance` to `Static`. But How can I remove the old data from `List` – Arunkumar Jun 19 '18 at 04:26
0
[TearDown]
    public void Cleanup()
    {
        typeof(FusionLicenseProvider).GetField("isShown ", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Static).SetValue(obj, false);
        typeof(FusionLicenseProvider).GetField("registery", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Static).SetValue(obj, New List<string>());
    }

These code is worked for me.

Arunkumar
  • 73
  • 1
  • 11