我正在尝试将一些单元测试合并到我试图重构(用于实践)的旧C#扑克游戏中。尝试测试非静态方法时遇到了麻烦,找不到任何解决方案。
相关类的类图看起来像this。
这是非静态方法的代码:
/// <summary>
/// Loops through the collection stored in cardArray[] and sets each of the card objects' inplay property to false.
/// </summary>
public void ResetUsage()
{
for (int i = 0; i < cardArray.Length; i++)
{
// Loops through the cardArray and sets inplay property to false.
cardArray[i].Inplay = false;
}
}
该数组为SuperCard类型,并在CardSet类的构造函数中定义。这就是它在Main()中的调用方式:
private static void RunPokerSession()
{
// Create our deck object
CardSet myDeck = new CardSet();
while (PokerSession.Balance != 0)
{
myDeck.ResetUsage();
// Retrieves the computer and player hands.
SuperCard[] computerHand = myDeck.GetCards(PokerSession.HandSize);
SuperCard[] playerHand = myDeck.GetCards(PokerSession.HandSize);
我正在尝试编写的单元测试正在检查是否对卡片组中的每张卡都将InPlay属性确实设置为false。
我的问题是是否可以使用NUnit编写此测试代码?
答案 0 :(得分:0)
是的,有可能。
如果您可以在junit测试中实例化CardSet类,则可以测试ResetUsage()是否达到您的期望。如果您可以观察到RunPokerSession对CardSet所做的操作,将取决于RunPokerSession的设计和结构,您的问题尚不清楚。您很可能希望将CarSet注入RunPokerSession,然后观察结果。您可以使用当前的实现方式做到这一点吗?
答案 1 :(得分:0)
在Lasse和Jocke的帮助下,我得以仔细考虑了实施过程。这是我为非静态方法实施单元测试的方式:
gcr.io