打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
对MVC的Controller进行模拟单元测试
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using MVCDomain.Abstract;
using MVCDomain;
using System.Collections.Generic;
using MvcDemo.Controllers;
using System.Linq;

namespace MvcDemo.Tests
{
    [TestClass]
    public class UnitTest
    {
        [TestMethod]
        public void TestMethod()
        {
            //Moq对象创建模拟器
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(s => s.Products).Returns(new List<Product>
            {
                new Product {ProductID=1, Name="Football",Price=25,Description="足球"},
                    new Product {ProductID=3, Name="Surf board",Price=179,Description="板球"},
                    new Product {ProductID=2, Name="Running shoes",Price=95,Description="跑步鞋"},
                    new Product {ProductID=4, Name="Rub shoes",Price=150,Description="轮滑鞋"},
                    new Product {ProductID=6, Name="Tom shoes",Price=190,Description="休闲鞋"},
                    new Product {ProductID=5, Name="LiNing shoes",Price=100,Description="李宁鞋"}
            });
            ProductController controller = new ProductController(mock.Object);
            controller.pageSize = 5;
            //动作
            IEnumerable<Product> result = (IEnumerable<Product>)controller.List(2).Model;
            //断言
            var list = result.ToList();
            Assert.IsTrue(list.Count==1);
            Assert.AreEqual(list[0].Name, "Tom shoes");
        }
    }
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
plsql ORA
用jsp实现一个简单的购物车web应用系统。
SAP UI5 里如何让每次视图显示时都执行某方法
检索出 IList<T> 或 List<T>中的不重复数据 Distinct()
The Timberland Company
如何在数据层分页以提高性能 (2005)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服