2017-05-17 193 views
1

我學習創建一個銀行應用程序單元測試方法爲我分配的一部分,我有一個問題:C#單元測試方法MOQ沒有測試

的測試方法GetAccounts()沒有被在Visual Studio中由於某種原因進行了測試。輸出消息我得到的是

「發現測試完成:0找到」

這是下面的測試方法塊。

[TestMethod] 
public void GetAccounts() 
{ 
    var testAccount = this.MockDatabase.GetAccounts(); 
    Assert.IsNotNull(testAccount); 
    Assert.AreEqual(4, testAccount.Count); 
} 

如何讓Visual Studio發現測試並給我一些結果?

如果有人希望我發佈更多我的代碼,而不僅僅是上面的代碼片段,請告訴我。我很樂意爲您提供更多信息。

+1

有類也是一個屬性? –

+0

確保測試類也具有'[TestClass]'屬性。顯示完整的測試。 – Nkosi

+0

啊是的,它確實有一個屬性。我沒有把它包含在那段代碼中。但是,謝謝。 – crhodes

回答

1

確保測試類也有[TestClass]屬性

[TestClass] //<--- Test classes must have this attribute to discover test methods 
public class AccountTests { 
    IDatabase MockDatabase; 

    [TestInitialize] 
    public void Arrange() { 

     var accounts = new List<Account> 
     { 
      new Checking(new Customer(1, "Alex", "Parrish"), 12, 30.00M), 
      new Savings(new Customer(2, "Alex", "Russo"), 12, 29.00M), 
      new Checking(new Customer(3, "Emma", "Swan"), 12, 30.00M), 
      new Savings(new Customer(4, "Henry", "Mills"), 12, 30.00M) 
     }; 

     var dataMock = new Mock<IDatabase>(); 
     dataMock.Setup(_ => _.GetAccounts()).Returns(accounts); 

     //...code removed for brevity 

     MockDatabase = dataMock.Object; 
    } 

    [TestMethod] 
    public void GetAccounts() { 
     var testAccount = this.MockDatabase.GetAccounts(); 
     Assert.IsNotNull(testAccount); 
     Assert.AreEqual(4, testAccount.Count); 
    } 

    //...code removed for brevity 
} 
+0

啊,是的,它確實有一個屬性。我沒有把它包含在那段代碼中。但是,謝謝。 – crhodes