gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:40 24 4
gpt4 key购买 nike

你好,我尝试添加单元测试,但看起来比我想象的要难 :( 有没有人可以帮助我并解释如何进行单元测试?

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();

}

}

我确实使用“创建单元测试”进行创建,但这里对我来说变得很难,我迷路了,而且不像谷歌教程那样容易。

[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]);
}

}

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

关于c# - 单元测试 C# 创建第一个测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20673648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com