我有以下的單元測試:單元測試只傳遞在調試模式下,無法在運行模式
[TestMethod]
public void NewGamesHaveDifferentSecretCodesTothePreviousGame()
{
var theGame = new BullsAndCows();
List<int> firstCode = new List<int>(theGame.SecretCode);
theGame.NewGame();
List<int> secondCode = new List<int>(theGame.SecretCode);
theGame.NewGame();
List<int> thirdCode = new List<int>(theGame.SecretCode);
CollectionAssert.AreNotEqual(firstCode, secondCode);
CollectionAssert.AreNotEqual(secondCode, thirdCode);
}
當我在調試模式下運行它,我的代碼通過了測試,但是當我運行測試正常(運行模式)它沒有通過。拋出的異常是:
CollectionAssert.AreNotEqual failed. (Both collection contain same elements).
這裏是我的代碼:
// constructor
public BullsAndCows()
{
Gueses = new List<Guess>();
SecretCode = generateRequiredSecretCode();
previousCodes = new Dictionary<int, List<int>>();
}
public void NewGame()
{
var theCode = generateRequiredSecretCode();
if (previousCodes.Count != 0)
{
if(!isPreviouslySeen(theCode))
{
SecretCode = theCode;
previousCodes.Add(previousCodes.Last().Key + 1, SecretCode);
}
}
else
{
SecretCode = theCode;
previousCodes.Add(0, theCode);
}
}
previousCodes是在類的屬性,其數據類型是字典關鍵整數,整數的值列表。 SECRETCODE也對類的屬性,其數據類型是整數
的列表如果我是做一個猜測,我會說的原因是NewGame()方法再次被調用,而第一個電話並沒有真正完成它需要做的事情。如您所見,還有其他方法在NewGame()方法中調用(例如generateRequiredSecretCode())。
在調試模式下運行時,我按下F10的速度緩慢,給進程結束足夠的時間。
但我真的不知道如何解決這個問題,假設我正確地找出原因。
該方法的代碼可以在這裏找到[http://pastebin.com/UNdYQvVy]。正如你所看到的,它不會創建一個三位數字(我需要的),其中有重複的數字(例如882 <壞),但是(392好)。 – Ciwan
已更新爲顯示構造函數。我明白你的意思是重複的嗎?還是我錯了? – Ciwan
我的意思是它在第一次呼叫時產生'123',例如在第二次呼叫時產生'123'。 – AShelly