c# – Internals,InternalsVisibleTo和测试共享行为
我们有一个共享的基本接口公共接口IOperation {…}和很多(几十个,很快就有100个)实现IOperation的不同对象.
我们还对所有这些实现进行了测试,所有这些实现都继承自名为TestOperationCommon的基类,这些基类使用实际操作类进行模板化,并将Create方法的类型模板化为新的此类操作.我们为TestOperationCommon实现了一个最多五个模板参数(这对所有操作都足够了). 现在,最近决定将所有操作实现内部并且只公开IOperation,这似乎是一个好主意,因为那些操作是实现细节.而[InternalsVisibleTo(…)]测试似乎也解决了. 但是,现在我看到我们不能再使用我们的测试结构,因为公共测试类的通用参数现在是内部的(至少是测试中的实际类),这导致 可访问性不一致….比…更难访问 错误.下面的代码,公共测试类不能从TestOperationCommon继承内部的泛型参数T.但是将所有这些共享行为测试复制到特定测试中似乎也是一个坏主意. >有没有办法让vstest框架(VS2013)测试内部的[TestClass]? 代码示例作为评论中的请求: public interface IOperation { ... } internal class SomeOperation : IOperation { public SomeOperation(A a,B b,C c) {...} } public abstract TestOperationCommon<T,A,B,C> where T : IOperation where ... { protected abstract T Create(A a,C c); [TestMethod] public void TestCommonOperationBehavior() { var op = Create(Mock.Of<A>(),Mock.Of<B>(),Mock.Of<C>); ... } } [TestClass] public class TestSomeOperation : TestOperationCommon<SomeOperation,...> { [TestMethod] public void TestSpecificSomeOperationStuff() {} } 解决方法
你能创建一个测试包装类吗?
就像是: [TestClass] public class AnnoyingTestSomeOperationWrapper { [TestMethod] public void TestSpecificSomeOperationStuff() { new TestSomeOperation().TestSpecificSomeOperationStuff() } } internal class TestSomeOperation : TestOperationCommon<SomeOperation,...> { public void TestSpecificSomeOperationStuff() {} } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |