2013-12-19 79 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]); 
    } 

} 

在大多數情況下,您將創建多個測試類似上面的單一方法,測試不同的方案,以確保你回來的預期結果。