Note that there are some explanatory texts on larger screens.

plurals
  1. POMoq - Create mocked instance for constructor injected object
    text
    copied!<h2>Question</h2> <p>I want to create a unit test for <code>Credentials</code> class that verifies the password for a given credentials after executing <code>Calculate()</code> method it its equal to the password calculated by an object that implements <code>IPasswordCalculator</code> interface.</p> <p>Actually, the mock object that I created returns <code>string.Empty</code> value when calls <code>Calculate(ICredentials)</code> method, instead of <code>Pa$$w0rd</code>, that would be the desired return.</p> <p>Then...</p> <ul> <li>How I can implement this test without having to manually create a stub class?</li> <li>How I can define the callback method on mock object to always return <code>Pa$$w0rd</code>?</li> </ul> <p>Thanks in advance.</p> <h2>Code</h2> <p><strong>IPasswordCalculator interface</strong></p> <pre><code>/// &lt;summary&gt; /// The password calculator interface. /// &lt;/summary&gt; public interface IPasswordCalculator { /// &lt;summary&gt; /// Calculates the user password using /// &lt;paramref name="credentials"/&gt; instance. /// &lt;/summary&gt; /// &lt;param name="credentials"&gt; /// &lt;see cref="ICredentials"/&gt; to perform the calculation. /// &lt;/param&gt; void Calculate(ICredentials credentials); } </code></pre> <p><strong>ICredentials interface</strong></p> <pre><code>/// &lt;summary&gt; /// The Credentials interface. /// &lt;/summary&gt; public interface ICredentials { /// &lt;summary&gt; /// Gets or sets the user password. /// &lt;/summary&gt; string Password { get; set; } /// &lt;summary&gt; /// Gets the user name. /// &lt;/summary&gt; string Username { get; } /// &lt;summary&gt; /// Calculates the password. /// &lt;/summary&gt; void Calculate(); } </code></pre> <p><strong>Credentials implementation</strong></p> <pre><code>/// &lt;summary&gt; /// User credentials. /// &lt;/summary&gt; public sealed class Credentials : ICredentials { /// &lt;summary&gt; /// Initializes a new instance of the &lt;see cref="Credentials" /&gt; class. /// &lt;/summary&gt; /// &lt;param name="passwordCalculator"&gt; /// The credentials calculator. /// &lt;/param&gt; /// &lt;param name="username"&gt; /// The user name. /// &lt;/param&gt; public Credentials(IPasswordCalculator passwordCalculator, string username) { this.Calculator = passwordCalculator; this.Username = username; this.Password = string.Empty; } /// &lt;summary&gt; /// Calculates the password. /// &lt;/summary&gt; public void Calculate() { if (null != this.Calculator) { this.Calculator.Calculate(this); } } } </code></pre> <p><strong>Test</strong></p> <pre><code> /// &lt;summary&gt; /// Calculate should set password value expected using calculator. /// &lt;/summary&gt; [TestMethod] public void CalculateShouldSetPasswordValueExpectedUsingCalculator() { ICredentials credentials = null; var repo = new MockRepository(MockBehavior.Default); var mock = repo.Create&lt;IPasswordCalculator&gt;(); mock.Setup(x =&gt; x.Calculate(credentials)) .Callback(() =&gt; { credentials.Password = "Pa$$w0rd"; }); IPasswordCalculator calculator = mock.Object; credentials = new Credentials(calculator, "Me"); credentials.Calculate(); Assert.AreEqual("Pa$$w0rd", credentials.Password); // Fail } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload