mercredi 2 janvier 2019

How to test multiple objects of the same type in NUnit

I am currently trying to learn NUnit for the 1st time.

For a C# program I want to develop using TDD, I have decided I want to write a User class to start with. This user class will (semantically) look like this:

using System;
namespace SSH_file_drop
{
public class User
{
    private Boolean authenticated = false;

    public string userName = null;

    //one-time object 'transmission' instance to allow single-use file transmission field
    Transmission fileVehicle = null;

    //property to discern whether user has been correctly authenticated
    public Boolean isAuthenticated {
        get;
        }

    public Boolean canSend ()
    {
        if (authenticated)
        {
            return this.userType != "sender";
        }
        return false;
    }

    public User(String inputUName)
    {
        String userName = inputUName;
    }

    public static void generateUser(string userName)
    {
        //contact server to attempt to register new user
    }

    public void ChangePassword(String oldPassword, String newPassword)
    {
        //ask server to change user password
    }

    public Boolean SetUpTransmission()
    {
        if (canSend())
        {
            try
            {
                fileVehicle = new Transmission(this);
                return true;
            }
            catch (e)
            {
                //write exception message
                return false;
            }
        }
        else
        {
            return false;
        }
    }
}

}

Really just placeholder code at the moment.

In order to test the User class however I am trying to write a TestFixture that creates separate instances of the User class, and somehow stores them persistently until teardown, enacting the same tests upon each object.

My idea was to create an array of User objects as a TestCase data source to test everything in order via the [Order(<n>)] attribute (bar User instance initialisation test method), but I have read here that a new instance of the TestFixture will be created for each method within it at runtime, so I would not be able to modify persistent data in a Test Fixture in this way.

Since I am trying to implement stateful logic - User object isAuthenticated() (and this authentication is dependent for subsequent tests after this as all User data is modelled as being stored in a remote database), is there a way to proceed without creating tests with repeated operations (create object, authenticate,check userType etc.), and thus multiple assertions?

Aucun commentaire:

Enregistrer un commentaire