Moq - Erstellen verspottet Instanz-Konstruktor injiziert Objekt

Frage

Möchte ich zum erstellen einer unit test für Credentials Klasse, die überprüft, ob das Kennwort für eine bestimmte Anmeldeinformationen nach der Ausführung Calculate() Methode es seinen gleich das Passwort berechnet, indem ein Objekt, die IPasswordCalculator - Schnittstelle.

Eigentlich das mock-Objekt, das ich erstellt zurück string.Empty Wert, wenn Anrufe Calculate(ICredentials) Methode, statt Pa$$w0rd werden würde, wäre die gewünschte Rückkehr.

Dann...

  • Wie kann ich implementieren, diese zu testen, ohne dass Sie manuell erstellen Sie eine stub-Klasse?
  • Wie kann ich das definieren der callback-Methode auf mock-Objekt, um immer wieder Pa$$w0rd?

Vielen Dank im Voraus.

Code

IPasswordCalculator Schnittstelle

///<summary>
///    The password calculator interface.
///</summary>
public interface IPasswordCalculator
{
    ///<summary>
    ///Calculates the user password using 
    ///<paramref name="credentials"/> instance.
    ///</summary>
    ///<param name="credentials">
    ///<see cref="ICredentials"/> to perform the calculation.
    ///</param>
    void Calculate(ICredentials credentials);
}

ICredentials-Schnittstelle

///<summary>
///    The Credentials interface.
///</summary>
public interface ICredentials
{
    ///<summary>
    ///    Gets or sets the user password.
    ///</summary>
    string Password { get; set; }

    ///<summary>
    ///    Gets the user name.
    ///</summary>
    string Username { get; }

    ///<summary>
    ///    Calculates the password.
    ///</summary>
    void Calculate();
}

Anmeldeinformationen Umsetzung

///<summary>
///    User credentials.
///</summary>
public sealed class Credentials : ICredentials
{
    ///<summary>
    ///    Initializes a new instance of the <see cref="Credentials" /> class.
    ///</summary>
    ///<param name="passwordCalculator">
    ///    The credentials calculator.
    ///</param>
    ///<param name="username">
    ///    The user name.
    ///</param>
    public Credentials(IPasswordCalculator passwordCalculator, string username)
    {
        this.Calculator = passwordCalculator;
        this.Username = username;
        this.Password = string.Empty;
    }

    ///<summary>
    ///    Calculates the password.
    ///</summary>
    public void Calculate()
    {
        if (null != this.Calculator)
        {
            this.Calculator.Calculate(this);
        }
    }
}

Test

    ///<summary>
    ///Calculate should set password value expected using calculator.
    ///</summary>
    [TestMethod]
    public void CalculateShouldSetPasswordValueExpectedUsingCalculator()
    {
        ICredentials credentials = null;
        var repo = new MockRepository(MockBehavior.Default);
        var mock = repo.Create<IPasswordCalculator>();
        mock.Setup(x => x.Calculate(credentials))
            .Callback(() => { credentials.Password = "Pa$$w0rd"; });

        IPasswordCalculator calculator = mock.Object;

        credentials = new Credentials(calculator, "Me");
        credentials.Calculate();

        Assert.AreEqual("Pa$$w0rd", credentials.Password);  //Fail
    }
InformationsquelleAutor HuorSwords | 2013-11-06
Schreibe einen Kommentar