mardi 26 février 2019

How to add a reference to a Form in NUnit unit testing, in C#?

I have a Form in C#, which has the following function:

    public bool SetAutomaticDownloadTimerEngine(int DownloadAtInterval)
    {
     //Do something here
    }

I want to test its behaviour using NUnit testing. For this, I had created a new project, added the reference and so on. In the NUnit test function, the code is this:

    [Test]
    public void SetTimesEngineTest()
    {
        //Arrange
        int flag = 1;
        bool Result = true;
        Form1 f = new Form1();

        //Act
        for (flag = -5; flag <= 10; flag++)
          Result = f.SetAutomaticDownloadTimerEngine(flag);

        //Assert
        if (flag == 1)
            Assert.AreEqual(Result, true);
        else
            Assert.AreEqual(Result, false);
        Assert.Pass();
    }

When I try to build this unit, I get the following error:

Error CS0012: The type 'Form' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

I tried adding System.Windows.Forms by using:

using System.Windows.Forms;

but I get the following error:

Error CS0234 The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)

What am I missing here?

Aucun commentaire:

Enregistrer un commentaire