如何为从基类继承的所有类进行通用单元测试?
我正在创建一个基类(或基实体),因为我的所有数据库表都将有一个companyID 字段。
在我的单元测试中,我必须确保companyID 值正确。
如果我返回对象列表,则所有 companyID
应该相同。
有没有一种通用的方法可以让我编写一个循环遍历所有值的测试,以便我可以在所有对象中使用它? (它们都将从具有 companyID
属性的基类继承)。
I am creating a base class (or base entity) since all my database tables will have a companyID
field.
In my unit tests, I have to make sure the companyID
value is correct.
If I am returning a list of objects, all the companyID
s should be the same.
Is there a generic way of me writing a test that will loop through all the values, that I could use across all my objects? (they will all inherit from a base class that will have a companyID
property).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的; 编写一个函数,该函数将循环遍历列表中的所有对象,将对象转换为基类,验证转换是否成功,并测试 CompanyID 是否有效。 您可能必须为每种检索对象列表的方式编写不同的单元测试类,但是一旦收到列表,您就可以从每个测试中调用一个通用函数。
Yes; write a function that will loop through all the objects in a list, cast the objects to the base class, verify the cast was successful, and test that the CompanyID is valid. You may have to write different unit test classes for each way of retrieving the list of objects, but once the list is received, you can call a common function from each of those tests.
一种方法是实现一个具有 companyId 字段的接口,然后返回该接口的列表,这样您就不必担心实际类型是什么
one way would be to implement an interface that has the companyId field, then you return a list of that interface, that way you don't have to worry about what the actual type is