简体   繁体   English

模拟调用SQL Server存储过程?

[英]Mocking calls to SQL Server Stored Procedures?

I am implementing an interface into a C# class, whose entire job is basically just to call some T-SQL stored procedures and return the data. 我正在实现C#类的接口,该类的整个工作基本上只是调用一些T-SQL存储过程并返回数据。 Other implementations of the interface may obtain data through web-services, reading files, etc, so to test this particular class I'd ideally mock an SQL Server database and its procedures. 该接口的其他实现可以通过Web服务,读取文件等获取数据,因此,为了测试该特定类,我理想地模拟SQL Server数据库及其过程。

I'm not sure if this is feasible. 我不确定这是否可行。 I've seen tools like RhinoMock used to mock database tables but since the entire purpose of my code is to talk to a DB, can I mock the entire DB or is that a bit of a waste of time? 我已经看到过像RhinoMock这样的工具用于模拟数据库表,但是由于我的代码的全部目的是与数据库对话,因此我可以模拟整个数据库还是浪费时间? I'd ideally like a way to transparently provide a replacement for having a real DB so local testing can be done, making real stored-procedure calls against a fake DB. 理想情况下,我想要一种透明地替代具有真实数据库的方法,以便可以进行本地测试,从而对伪造的数据库进行真实的存储过程调用。

If you are building unit tests then you should not execute the stored procedure and you should stub this interface. 如果要构建单元测试,则不应执行存储过程,而应将此接口存根。

If you are building integration tests then you must have it all working. 如果要构建集成测试,则必须使其全部正常工作。

In both cases your class shouldn't do it directly, you should have an inner handler like IDbHandler and during your unit tests you should mock it and during the integration tests you should use your concrete implementation. 在这两种情况下,您的类都不应该直接这样做,您应该有一个内部处理程序,例如IDbHandler,并且在单元测试期间应该模拟它,而在集成测试期间应该使用具体的实现。

The purpose of having a mock is to verify that the other side (DB in your case) got a request with the expected parameters but a physical component can't be mocked so just by adding an interface between your code and the physical component will enable those validations. 进行模拟的目的是验证另一端(在您的情况下为DB)具有预期参数的请求,但是无法模拟物理组件,因此只需在代码和物理组件之间添加接口即可启用这些验证。

BTW I would start with having the unit tests and just afterwards add the integration tests. 顺便说一句,我将从进行单元测试开始,然后再添加集成测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM