Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you expose the injected <code>clientID</code> (and <code>dependency</code> as well) as read-only properties, you can always query their values:</p> <pre><code>public class BuilderStrategy { private readonly string _clientID; private readonly IDependency _dependency; public void BuilderStrategy(string clientID, IDependency dependency) { _clientID = clientID; _dependency = dependency; } public string GetClientExtension() { return _clientID.Substring(_clientID.LastIndexOf("-") + 1); } public string ClientID { get { return _clientID; } } public IDependency Dependency { get { return _dependency; } } } </code></pre> <p>This <a href="http://blog.ploeh.dk/2013/04/04/structural-inspection" rel="noreferrer"><em>doesn't</em> break encapsulation, but is rather known as Structural Inspection</a>.</p> <p>With this change, you could now rewrite the test like this:</p> <pre><code>[Theory, MyConventions] public void GetClientExtensionReturnsCorrectValue(BuilderStrategy sut) { var expected = sut.ClientID.Substring(sut.ClientID.LastIndexOf("-") + 1); var actual = sut.GetClientExtension(); Assert.Equal(expected, actual); } </code></pre> <p>Some people don't like duplicating production code in the unit test, but I would rather argue that if you follow Test-Driven Development, it's the production code that duplicates the test code.</p> <p>In any case, this is a technique known as <a href="http://xunitpatterns.com/Derived%20Value.html" rel="noreferrer">Derived Value</a>. In my opinion, <a href="http://blog.ploeh.dk/2013/04/02/why-trust-tests" rel="noreferrer">as long as it retains a cyclomatic complexity of 1, we can still trust the test</a>. Additionally, as long as the duplicated code only appears in two places, the <a href="http://en.wikipedia.org/wiki/Rule_of_three_%28computer_programming%29" rel="noreferrer">rule of three</a> suggests that we should keep it like that.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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