2013-12-19 78 views
2

你好,我尝试添加单元测试,但看起来像它的困难,我没有想到:(是否有任何人谁可以帮我解释一下如何使一个?单元测试C#创建第一个测试

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"); 
     } 
+1

更具体。什么不适合你。当您构建“Solution”/“Project”时,您的TestMethod应显示在“文本浏览器”视图中。 –

回答

1

嗯,我“不敢确定您的问题是,因为你不” t给出了很多信息,但我可以告诉你,你在一些自动生成的代码中复制了最重要的部分是

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

} 

在大多数情况下,您将创建多个测试类似上面的单一方法,测试不同的方案,以确保你回来的预期结果。