惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - 踏歌长行

前端知识质量内容网址 NuGet 质量博客链接 Entity Framework 质量博客链接 LINK1123:failure during conversion to COFF:file invalid or corrupt 未授权用户在此计算机上的请求登录类型 System.ServiceModel.AddressAccessDeniedException DataGrid 导出 Excel 中文乱码 80070005 Access is denied in Windows 2008 How to install ASP.NET 1.1 with IIS7 on Vista and Windows 2008 Unable to find script library '/aspnet_client/system-web/1-1-4322/webvalidation.js' JQuery file upload Access is denied in IE 7, 8, 9 ReportViewer 自适应高度 单元测试之 Xunit ASP.NET MVC ajax 提交列表到 Action VS 2010 制作 Windows Service 安装包之用户界面 VS 2010 制作 Windows Service 安装包 Postback 之后保持浏览器滚动条的位置 VS 2010 开发 ActiveX 自动升级外篇 VS 2010 开发 ActiveX 制作 cab 包
单元测试之模拟Mock
踏歌长行 · 2014-01-15 · via 博客园 - 踏歌长行



先看下面一段代码:

public class DataService : IDataService
{
        private readonly IDataRespository _dataRespository;
        public DataService(IDataRespository dataRespository)
        {
            _dataRespository = dataRespository;
        }

        public int GetCount()
        {
            var list = _dataRespository.GetList();
            return list.Count;
        }
}

其中有 GetCount() 方法是为获取列表的 Count,我们为这个方法写单元测试代码;GetCount() 中获取列表是调用了 IDataRespository 中的 GetList() 方法,此方法中的具体实现、返回的数据量我们都一无所知,所以为了测试 GetCount() 逻辑的正确性,必须对 GetList() 方法进行模拟。
1. 项目中引入Moq.dll
2. 具体如下

[Fact]
public void TestGetList()
{
            // 为 IDataRespository 创建模拟对象
            var mockDataRespository = new Mock<IDataRespository>();
            // 设置模拟对象的 GetList() 方法并设置返回值
            mockDataRespository.Setup(p => p.GetList()).Returns(() =>
                {
                    var list = new List<DataModel> {new DataModel()};

                    return list;
                });

            IDataService dataService = new DataService(mockDataRespository.Object);

            var actual = dataService.GetCount();
            const int expect = 1;

            Assert.Equal(expect, actual);
}