HuorSwords
HuorSwords

Reputation: 2255

Moq - Create mocked instance for constructor injected object

Question

I want to create a unit test for Credentials class that verifies the password for a given credentials after executing Calculate() method it its equal to the password calculated by an object that implements IPasswordCalculator interface.

Actually, the mock object that I created returns string.Empty value when calls Calculate(ICredentials) method, instead of Pa$$w0rd, that would be the desired return.

Then...

Thanks in advance.

Code

IPasswordCalculator interface

/// <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 interface

/// <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();
}

Credentials implementation

/// <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
    }

Upvotes: 4

Views: 9248

Answers (2)

twoflower
twoflower

Reputation: 6830

You need to setup your mock in the following way

mock.Setup(x => x.Calculate(It.IsAny<ICredentials>()))
    .Callback((ICredentials c) => { c.Password = "Pa$$w0rd"; });

Thus you tell it "whatever ICredentials you get as the parameter of the Calculate method, set its Password to 'Pa$$word'"

Upvotes: 5

Matthias
Matthias

Reputation: 1799

I think you need to initialize the credentials variable before the call to mock.Setup(). With your code, Moq expects a call credentials.Calculate(null).

Upvotes: 0

Related Questions