Reputation: 153
Hello I try to add unit test but looks like its harder that I did think :( Is there anyone who can help me and explain how to make one ?
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();
}
}
I did create using "create unit test" but here its becomes to hard for me, im lost and its not that easy like on google tutorials.
[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");
}
Upvotes: 2
Views: 358
Reputation: 184
Well I"m not really sure where your problem is since you don't give much information, but I can tell that you copied in some auto generated code the most important part is
public void USerServiceConstructorTest()
{
USerService_Accessor target = new USerService_Accessor();
Assert.Inconclusive("TODO: Implement code to verify target");
}
the above method should be used to test your method
public List<UserInfo> GetUserU()
{
QuizDBEntities contex = new QuizDBEntities();
var userU = from a in contex.UserInfoes select a;
return userU.ToList();
}
your particular method doesn't have much to test and really it should be changed to make it easier to test but that's a different subject.
if you wanted to make sure that GetUserU was returning only one user you could test it like this
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);
}
The assert statement is used to specify what your are testing. Although I don't think the above will work as is because I'm asserting equality of two list types. maybe better to do something like this
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]);
}
}
in most cases you will create multiple test like the one above for a single method testing different scenarios to make sure that you get back an expected result.
Upvotes: 1