Visual Studio 2008:未正确检测泛型类中的单元测试方法

发布于 2024-09-24 07:52:31 字数 1008 浏览 0 评论 0原文

我有一堆存储库类,我想使用 Visual Studio 2008 对它们进行单元测试。它们实现以下接口:

public interface IRepository<TEntity> where TEntity : IEntity
{
    /// <summary>
    /// Get entity by ID
    /// </summary>
    /// <param name="id">The ID</param>
    /// <returns></returns>
    TEntity GetById(int id);

    /// <summary>
    /// Get all entities
    /// </summary>
    /// <returns></returns>
    IEnumerable<TEntity> GetAll();
}

现在我可以为我拥有的每个存储库编写一个完全成熟的测试类。然而,为了最大限度地减少冗余,我想编写一个基本测试类,其中包含主要的“通用”测试方法。这将允许我为每个存储库编写一个简单的子类,如下所示:

[TestClass]
public class EmployeeRepositoryTest : RepositoryTestBase<Employee>
{
     // all test methods are inherited from the base class
     // additional tests could be added here...
}

但是,Visual Studio 无法检测到 RepositoryTestBase 中定义的测试方法(因为泛型),使得这种方法毫无用处。为了使其工作,我需要包装基类的每个方法,使它们对 Visual Studio 可见,这又会导致冗余。

是否有更好的方法来解决这个问题?我不想用大量的包装代码来扰乱我的测试:(

I have a bunch of repository classes which I want to unit-test using Visual Studio 2008. They implement the following interface:

public interface IRepository<TEntity> where TEntity : IEntity
{
    /// <summary>
    /// Get entity by ID
    /// </summary>
    /// <param name="id">The ID</param>
    /// <returns></returns>
    TEntity GetById(int id);

    /// <summary>
    /// Get all entities
    /// </summary>
    /// <returns></returns>
    IEnumerable<TEntity> GetAll();
}

Now I could write a fully blown test class for each and every repository I have. However to minimize redundancies, I want to write a base test class which contains the main "generic" test methods. This would allow me to write a simple subclass for every repository, like this:

[TestClass]
public class EmployeeRepositoryTest : RepositoryTestBase<Employee>
{
     // all test methods are inherited from the base class
     // additional tests could be added here...
}

However, the test methods defined in RepositoryTestBase are not detected by Visual Studio (because of generics), making this approach useless. To make it work, I would need to wrap each method of the base class to make them visible to Visual Studio, which causes redundancies again..

Is there a better approach to solve this pain? I don't want to clutter my tests with tons of wrapping code :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无边思念无边月 2024-10-01 07:52:31

测试声明中是否可以不使用泛型,而是单独依赖 IEntity,例如:

IEntity GetById(int id);
IEnumerable<IEntity> GetAll();

如果这两个函数需要对 IEntity 进行任何实例化,则可以让存储库构造函数接受工厂对象。工厂对象的工作就是进行实例化。工厂会有一个抽象基(或接口),其实例化方法仅返回 IEntity,以及可能会也可能不会模板化的子类。

Is it possible to not have generics in the testing declaration, instead relying on IEntity alone, like:

IEntity GetById(int id);
IEnumerable<IEntity> GetAll();

And if those two functions need to do any instantiation of IEntity, you could have the Repository constructor accept a factory object. It would be the factory object's job to do the instantiation. There would be an abstract base (or interface) for the factory whose instantiation method just returns an IEntity, and child classes that may or may not be templated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文