我有以下代码:
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<T>
{
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<A>
{
public Bar(IStatementExecutor statementExecutor)
: base(statementExecutor, "ATable")
{
}
}
public class Baz : Foo<B>
{
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<IStatementExecutor>();
mockStatementExecutor.Expect(m => m.Exec("sp_" + tableName + "Update"));
var sut = MockRepository.GeneratePartialMock<Foo<A>>(mockStatementExecutor, tableName);
var testModel = new A();
sut.Update(testModel);
mockStatementExecutor.AssertWasCalled(m => m.Exec("sp_" + tableName + "Update"));
}
}
我已经对基类进行了单元测试Foo<T>
。由于已经涵盖了基类,我不想为派生类Bar
和Baz
.
我在派生类中唯一真正关心的是正确的字符串target
是否传递给基类。
我正在努力解决如何在不破坏派生类封装或编写冗余单元测试的情况下对其进行单元测试。
所以,问题是,我如何测试正确的值是否从派生类传递给基类的target
参数?
(如果您的答案是“使用组合...”,请使用从上面修改的代码示例进行备份。
谢谢!