zoukankan      html  css  js  c++  java
  • NUnit+mock+moq单元测试

    [TestFixture]
        public class InstantBatchBuyTest
        {
            private string _mallAbc;
            private string _itemCode;
            private int _quantity;
            private string _items;
            Mock<ICatalogService> mockCatalog;
            Mock<IShoppingCartService> mockShoppingCartService;
    
            [SetUp]
            public void Init()
            {
                _mallAbc = string.Empty;
                _itemCode = "18-001-0001";
                _quantity = 2;
    
                _items = "[{"qty":2,"sku":"18-001-0001"},{"qty":4,"sku":"18-001-0002"},{"qty":2,"sku":"18-001-0003"}]";
    
                var items = new List<ItemInfo>() { new ItemInfo { ItemCode = "18-001-0001" }, new ItemInfo { ItemCode = "18-001-0002" }, new ItemInfo { ItemCode = "18-001-0003" } };
                mockCatalog = new Mock<ICatalogService>();
                mockCatalog.Setup(s => s.GetItemByCodeList(It.IsAny<List<string>>())).Returns(items); // 这些都是服务端的接口,此处我们可以设置返回值
    
                mockShoppingCartService = new Mock<IShoppingCartService>();
                mockShoppingCartService.Setup(s => s.CheckItemInventory("18-001-0001", 2)).Returns(true);
                mockShoppingCartService.Setup(s => s.CheckItemInventory("18-001-0002", 4)).Returns(true);
                mockShoppingCartService.Setup(s => s.CheckItemInventory("18-001-0003", 2)).Returns(true);
                mockShoppingCartService.Setup(s => s.InstantBuyForbiddenBuyProduct(items)).Returns(false);
            }
    
    
            [Test]
            public void 立即购买_单个商品()
            {
                var result = new InstantBuyAjaxResult();
                // Arrange
                var instanItemList = new List<InstantItemModel>() { new InstantItemModel { ItemCode = _itemCode, Quantity = _quantity } };
                if (instanItemList.Any())
                {
                    // Act
                    result =
                        (new InstantBuyLogic(mockShoppingCartService.Object, mockCatalog.Object)).InstantBuy(
                            instanItemList);
                }
    
                // Assert
                Assert.AreEqual(result.Status, 1);
            }
    
            [Test]
            public void 立即购买_多个商品()
            {
                var result = new InstantBuyAjaxResult();
                // Arrange
                var instanItemList = JsonHelper.JsonToObject<List<InstantItemModel>>(_items);
                if (instanItemList != null && instanItemList.Any())
                {
                    // Act
                    result =
                        (new InstantBuyLogic(mockShoppingCartService.Object, mockCatalog.Object)).InstantBuy(
                            instanItemList);
                }
    
                // Assert
                Assert.AreEqual(result.Status, 1);
            }
        }
    

    需要添加Moq.dll、nunit.framework.dll引用,开发过程中结合Resharper,使用起来更加方便

  • 相关阅读:
    前端开发者进阶之ECMAScript新特性--Object.create
    JS事件:target与currentTarget区别
    30分钟掌握ES6/ES2015核心内容
    百度跨域搜索demo
    <a>标签的SEO优化细节
    jQuery之异步Ajax请求使用
    小tips: zoom和transform:scale的区别
    JSP页面静态化总结之一使用URLRewrite实现url地址伪静态化
    web前端安全机制问题全解析
    【转】Asp.net MVC Comet推送
  • 原文地址:https://www.cnblogs.com/cr7/p/3229420.html
Copyright © 2011-2022 走看看