繁体   English   中英

单元测试C#创建第一个测试

[英]Unit testing C# creating first test

您好,我尝试添加单元测试,但看起来确实比我想的要难:(有没有人可以帮助我并说明如何制作单元测试?

public class USerService : IUSerService
{
    [DataMember]
    public int ID { get; set; }
    [DataMember]
    public string Login { get; set; }
    [DataMember]
    public string UserType { get; set; }

    public List<UserInfo> GetUserU()
    {
        QuizDBEntities contex = new QuizDBEntities();
        var userU = from a in contex.UserInfoes select a;

        return userU.ToList();

    }

}

我确实使用“创建单元测试”进行了创建,但是在这里,这对我来说变得很困难,我迷路了,它不像在Google教程上那么容易。

[TestClass()]
    public class USerServiceTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for USerService Constructor
        ///</summary>
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("C:\\Users\\Drage\\Desktop\\lekcja1\\Dunskiseba", "/Dunskiseba")]
        [UrlToTest("http://localhost/Dunskiseba")]
        public void USerServiceConstructorTest()
        {
            USerService_Accessor target = new USerService_Accessor();
            Assert.Inconclusive("TODO: Implement code to verify target");
        }

好吧,我不确定您的问题出在哪里,因为您没有提供太多信息,但是我可以告诉您,您在一些自动生成的代码中进行了复制,最重要的部分是

public void USerServiceConstructorTest()
{
    USerService_Accessor target = new USerService_Accessor();
    Assert.Inconclusive("TODO: Implement code to verify target");
}

上面的方法应该用来测试你的方法

public List<UserInfo> GetUserU()
{
    QuizDBEntities contex = new QuizDBEntities();
    var userU = from a in contex.UserInfoes select a;

    return userU.ToList();

}

您的特定方法没有太多要测试的方法,实际上应该对其进行更改以使其更易于测试,但这是一个不同的主题。

如果您想确保GetUserU仅返回一个用户,则可以像这样测试它

public void USerServiceConstructorTest()
{
    USerService_Accessor target = new USerService_Accessor();
    List<UserInfo> expected = new List<UserInfo>();

    expected.Add(new UserInfo{ Name = "made up"});

    actual = target.GetUserU();

    Assert.Equals(expected, actual);

}

assert语句用于指定要测试的内容。 尽管我认为上面的方法不起作用,因为我主张两种列表类型相等。 也许做这样的事情更好

public void USerServiceConstructorTest()
{
    USerService_Accessor target = new USerService_Accessor();
    List<UserInfo> expected = new List<UserInfo>();

    expected.Add(new UserInfo{ Name = "made up"});

    actual = target.GetUserU();

    Assert.Equals(expected.Count(), actual.Count());
    //here I'm going to assume they are sorted
    for(int i = 0; i < expected.Count(); i++)
    {
        Assert.Equals(expected[i], actual[i]);
    }

}

在大多数情况下,您将针对单个方法创建多种测试,如上面的测试,以测试不同的场景,以确保获得预期的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM