Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit testing passing arguments to base constructor
    primarykey
    data
    text
    <p>I have the following code:</p> <pre><code> using System; using NUnit.Framework; using Rhino.Mocks; public class A { } public class B { } public interface IStatementExecutor { void Exec(string statement); } public abstract class Foo&lt;T&gt; { private readonly IStatementExecutor _statementExecutor; private readonly string _targetSegment; protected Foo(IStatementExecutor statementExecutor, string targetSegment) { _statementExecutor = statementExecutor; _targetSegment = targetSegment; } public void Update(T item) { _statementExecutor.Exec("sp_" + _targetSegment + "Update"); } } public class Bar : Foo&lt;A&gt; { public Bar(IStatementExecutor statementExecutor) : base(statementExecutor, "ATable") { } } public class Baz : Foo&lt;B&gt; { public Baz(IStatementExecutor statementExecutor) : base(statementExecutor, "BTable") { } } [TestFixture] public class Foo_Tests { [Test] public void Update_CallsStatementExecutorWithTableName() { const string tableName = "TestTable"; var mockStatementExecutor = MockRepository.GenerateMock&lt;IStatementExecutor&gt;(); mockStatementExecutor.Expect(m =&gt; m.Exec("sp_" + tableName + "Update")); var sut = MockRepository.GeneratePartialMock&lt;Foo&lt;A&gt;&gt;(mockStatementExecutor, tableName); var testModel = new A(); sut.Update(testModel); mockStatementExecutor.AssertWasCalled(m =&gt; m.Exec("sp_" + tableName + "Update")); } } </code></pre> <p>I already have unit tests for the base class <code>Foo&lt;T&gt;</code>. Since the base class is already covered, I don't want to write identical tests for the derived classes <code>Bar</code> and <code>Baz</code>. </p> <p>The only thing I really care about in the derived classes is that the correct string <code>target</code> is passed to the base class. </p> <p>I'm struggling on how to unit test this without breaking encapsulation of the derived classes or writing redundant unit tests. </p> <p>So, the question is, how do I test that the correct value gets passed to the base class from the derived classes for the <code>target</code> parameter?</p> <p>(If your answer is "use composition...", please back it up with a code sample modified from above.</p> <p>Thanks!</p>
    singulars
    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.
 

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