zoukankan      html  css  js  c++  java
  • ASP.NET MVC5 学习笔记-5 测试

    1. 测试步骤

    • 准备
    • 执行
    • 检查

    2. 创建单元测试

    注意:单元测试不要包含数据库操作,包含数据库操作的一般成为集成测试。

    2.1 编写测试代码

    namespace AspNetMVCEssential.Tests.Controllers
    {
        [TestClass]
        public class HomeControllerTest
        {
            [TestMethod]
            public void Index()
            {
                // Arrange
                HomeController controller = new HomeController();
    
                // Act
                ViewResult result = controller.Index() as ViewResult;
    
                // Assert
                Assert.IsNotNull(result);
            }
    
            [TestMethod]
            public void About()
            {
                // Arrange
                HomeController controller = new HomeController();
    
                // Act
                ViewResult result = controller.About() as ViewResult;
    
                // Assert
                Assert.AreEqual("Your application description page.", result.ViewBag.Message);
            }
    
            [TestMethod]
            public void Contact()
            {
                // Arrange
                HomeController controller = new HomeController();
    
                // Act
                ViewResult result = controller.Contact() as ViewResult;
    
                // Assert
                Assert.IsNotNull(result);
            }
        }
    }
    

    2.2 运行测试

    选定菜单“测试”->“运行”->运行所有测试或者未成功的测试。
    可以在“测试”->“窗口”->“测试资源管理器”中对测试进行管理。


    3 Mock测试

    Mock测试就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法。

    我们要对TransactionController.cs进行测试,直接操作数据库不行,怎么办呢?我们可以使用虚拟的接口来进行测试。这需要我们更改一些代码.

    3.1 首先,我们更改TransactionController.cs

    更改db为IApplicationDbContext类型,并且添加构造方法:

    private IApplicationDbContext db;
    
    //实际使用
    public TransactionController()
    {
        db = new ApplicationDbContext();
    }
    
    //生成使用
    public TransactionController(IApplicationDbContext dbContext)
    {
        db = dbContext;
    }
    

    3.2 创建IApplicationDbContext对象

    public interface IApplication
    {
        IDbSet<CheckingAccount> CheckingAccounts{get;set;}
        IDbSet<Transaction> Transactions{get; set;}
        
        public int SaveChanges();
    }
    

    让实际的ApplicationDbContext实现IApplicationDbContext。

    另外创建一个假的FakeApplicationDbContext实现IApplicationDbContext

    public class FakeApplicationDbContext:IApplicationDbContext
    {
        IDbSet<CheckingAccount> CheckingAccounts{get;set;}
        IDbSet<Transaction> Transactions{get; set;}
        
        public int SaveChanges(){return 0}
    }
    

    再创建一个FakeDbSet

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace EntityExtensions {
        public class FakeDbSet<T> : System.Data.Entity.IDbSet<T> where T : class {
            private readonly List<T> list = new List<T>();
    
            public FakeDbSet() {
                list = new List<T>();
            }
    
            public FakeDbSet(IEnumerable<T> contents) {
                this.list = contents.ToList();
            }
    
            #region IDbSet<T> Members
    
            public T Add(T entity) {
                this.list.Add(entity);
                return entity;
            }
    
            public T Attach(T entity) {
                this.list.Add(entity);
                return entity;
            }
    
            public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, T {
                throw new NotImplementedException();
            }
    
            public T Create() {
                throw new NotImplementedException();
            }
    
            public T Find(params object[] keyValues) {
                throw new NotImplementedException();
            }
    
            public System.Collections.ObjectModel.ObservableCollection<T> Local {
                get {
                    throw new NotImplementedException();
                }
            }
    
            public T Remove(T entity) {
                this.list.Remove(entity);
                return entity;
            }
    
            #endregion
    
            #region IEnumerable<T> Members
    
            public IEnumerator<T> GetEnumerator() {
                return this.list.GetEnumerator();
            }
    
            #endregion
    
            #region IEnumerable Members
    
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
                return this.list.GetEnumerator();
            }
    
            #endregion
    
            #region IQueryable Members
    
            public Type ElementType {
                get { return this.list.AsQueryable().ElementType; }
            }
    
            public System.Linq.Expressions.Expression Expression {
                get { return this.list.AsQueryable().Expression; }
            }
    
            public IQueryProvider Provider {
                get { return this.list.AsQueryable().Provider; }
            }
    
            #endregion
        }
    }
    

    另外一种方法是使用

    Install-Package FakeDbSet
    

    4 TDD

    4.1 TDD基本流程

    创建失败的测试
    用最简单的方法实现
    优化

    4.2 创建测试

    注意,测试应该也引用EF。

  • 相关阅读:
    C#结构体指针的定义及使用详解
    C# out和ref关键字
    CListCtrl控件主要事件及LVN_ITEMCHANGED消息和鼠标双击列表项事件的处理
    深圳职位:替朋友招聘biztalk的技术人员 无为而为
    微软出最新SQL Server 2005 累积修补程序包(版本 9.0.2153)(200684),这是SP1之后的更新 无为而为
    微软Visual Studio 2005 SDK version 3.0 发布了,TFS扩展的开发人员/DSL/VSTS Test都应该关注 无为而为
    好东西,将你的英文版TFS变为中文版?:Visual Studio 2005 Team Foundation Server 语言更改包 无为而为
    Analysis Service计算[期间增长率]的若干注意 无为而为
    亲身体验:更改 Team Foundation Server 部署的语言版本是一个复杂耗时的过程 无为而为
    各大门户调查:鄙视新浪,将博士伦的广告挂在热点新闻中间好几天了,把大众当傻瓜 无为而为
  • 原文地址:https://www.cnblogs.com/liulixiang/p/4299316.html
Copyright © 2011-2022 走看看