如何测试查询数据库而不实际执行查询的方法? [英] How to test a method that queries a database without actually performing the query?

查看:154
本文介绍了如何测试查询数据库而不实际执行查询的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个单元测试,将执行检查从一个方法返回的字符串。该字符串是实体的一部分,应由对数据库的调用生成。我可以轻松地存根这样的实体,但我不知道如何省略对数据库的调用,因为它是方法的一部分。

I am trying to write a unit test that will perform a check against a string returned from a method. That string is a part of an entity, which should be generated by a call to the database. I can easily stub such entity, but I don't know how to omit the call to the database, since it is a part of the method.

该方法看起来像this:

The method looks like this:

private string GetDescriptionForRelationEntry(string relAttrId, string client)
        {
            // here we are querying the database
            var relationEntities = new EntityDatabaseQuery<AttributeEntryEntity>();

            // the rest of the method
        }

部分指定为需要执行方法的其余部分,因为我必须获取字符串结果来执行必要的检查。所以,基本上,我需要假只有一行代码。

The part specified as the rest of the method needs to be executed, because I have to obtain the string result to perform necessary checks. So, basically, I need to "fake" only one line of code.

现在我不能说如何对SUT类进行大量的修改是允许的,因此我不限制这个问题到任何具体的解决方案。

Right now I'm not able to say how extensive modifications on the SUT class are allowed, thus I'm not limiting this problem to any specific solution. Any help will be appreciated.

推荐答案

您可以简单地创建一个模拟方法来表示数据库结果
使用此链接有关详情: http://msdn.microsoft.com/en-us/library /ff650441.aspx

You can simply create a mock method to represent your DB result Use this link for more details : http://msdn.microsoft.com/en-us/library/ff650441.aspx

这篇关于如何测试查询数据库而不实际执行查询的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆