zoukankan      html  css  js  c++  java
  • fakes的例子

    using System;
    using System.Text;
    using System.Collections.Generic;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Bangso.Domain;
    using Bangso.UI.Web.Controllers;
    using System.Transactions;
    using Bangso.Domain.UserInfos;

    namespace Bangso.UnitTest
    {
    /// <summary>
    /// ProductRepositroyTest 的摘要说明
    /// </summary>
    [TestClass]
    public class UserInfoProductRepositoryTest : _UnitTestBase
    {
    public UserInfoProductRepositoryTest()
    {

    }

    private TestContext testContextInstance;

    /// <summary>
    ///获取或设置测试上下文,该上下文提供
    ///有关当前测试运行及其功能的信息。
    ///</summary>
    public TestContext TestContext
    {
    get
    {
    return testContextInstance;
    }
    set
    {
    testContextInstance = value;
    }
    }

    [TestMethod]
    public void FindBy()
    {
    /*
    * 使用Mock测试的原因是由于接口没有被实现,所以调用接口的类就无法被调试
    * Mock的意义在于模拟接口的返回值,实际调试正确性的并不是接口,而是调用接口的类
    */

    var database = new UserInfo { Username = "xinux" };
    var repository = new Bangso.Domain.UserInfos.Fakes.StubIUserInfoRepository()
    {
    FindByString = model => database
    };

    var xx = repository.FindByString("xinux");

    Assert.AreEqual(xx.Username, "xinux");
    }
    }
    }

    ---------------------------------------

    using System;
    using System.Text;
    using System.Collections.Generic;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Bangso.Domain;
    using Bangso.UI.Web.Controllers;
    using System.Transactions;
    using Bangso.Domain.UserInfos;

    namespace Bangso.UnitTest
    {
    /// <summary>
    /// ProductRepositroyTest 的摘要说明
    /// </summary>
    [TestClass]
    public class UserInfoProductRepositoryTest : _UnitTestBase
    {
    public UserInfoProductRepositoryTest()
    {

    }

    private TestContext testContextInstance;

    /// <summary>
    ///获取或设置测试上下文,该上下文提供
    ///有关当前测试运行及其功能的信息。
    ///</summary>
    public TestContext TestContext
    {
    get
    {
    return testContextInstance;
    }
    set
    {
    testContextInstance = value;
    }
    }

    //初始化数据
    List<UserInfo> DataBase = new List<UserInfo>();
    [TestInitialize()]
    public void TestInitialize()
    {
    this.DataBase.Add(new UserInfo { Username = "xinux" });
    }

    [TestMethod]
    public void FindBy()
    {
    var repository = new Bangso.Domain.UserInfos.Fakes.StubIUserInfoRepository()
    {
    FindByString = model => this.DataBase[0]
    };

    var result = repository.FindByString("xinux");
    Assert.AreEqual(result.Username, "xinux");
    }

    [TestMethod]
    public void FindAll()
    {
    var repository = new Bangso.Domain.UserInfos.Fakes.StubIUserInfoRepository()
    {
    FindAll = () => this.DataBase
    };

    var result = repository.FindAll();
    Assert.AreEqual(result[0].Username, "xinux");
    }

    [TestMethod]
    public void Add()
    {
    var repository = new Bangso.Domain.UserInfos.Fakes.StubIUserInfoRepository()
    {
    AddUserInfo = (x) => { this.DataBase.Add(new UserInfo { Username = "bill"});
    Assert.AreEqual(this.DataBase.Count, 2);
    }
    };
    }

    [TestMethod]
    public void Update()
    {
    var database = new UserInfo { Username = "xinux" };
    var repository = new Bangso.Domain.UserInfos.Fakes.StubIUserInfoRepository()
    {
    UpdateUserInfoString = (x, y) =>
    {
    Assert.AreEqual(this.DataBase.Count, 2);
    }
    };
    }


    }
    }

  • 相关阅读:
    临时产品id记录
    一张图包含SEO一切要点
    Java基础之Volatile原理
    docker
    Linux设置虚拟内存教学和实战
    用最简单的话告诉你什么是ElasticSearch
    git全局配置修改
    数组和字符串方法区别
    git初级使用
    JavaScript的5中基本数据类型
  • 原文地址:https://www.cnblogs.com/yeagen/p/2954425.html
Copyright © 2011-2022 走看看