我想设置卡游戏应用程序的单元测试,但我的代码是抛出一个NullReferenceException:对象引用未设置为对象的实例。据我所知,我不应该得到这个错误,但它是。NullReferenceException当使用NUnit
这里是我的代码:
[TestFixture]
public class Tests
{
CardTable aTable = null;
[SetUp]
public void setup()
{
aTable = new CardTable();
}
[Test]
public void setPlayerGold_setTo0_return0()
{
//arrange
//act
aTable.setPlayerGold(0);
//assert
Assert.AreEqual(0, aTable.playerGold);
}
}
public class CardTable
{
int playerGold;
public CardTable()
{
playerGold = 0;
}
public void setPlayerGold(int amount)
{
if (amount == 0)
{
playerGold = 0;
}
else
{
playerGold += amount;
}
goldLabel.Text = playerGold + "";
}
的异常被在aTable.setup行抛出虽然aTable没有实例化,即使它显然是在[设置],我想不通为什么。
我使用NUnit 2.6.0.12051运行Visual C#2010 Express v10.0.40219.1 SP1Rel。
任何帮助,将不胜感激。 谢谢!
调试过程中,设置是否被调用? –
我们可以看到CardTable类的代码吗?看来这个设置崩溃了。 – ykatchou
@ykatchou,CardTable类不是问题,因为它运行时工作正常。问题是NUnit认为CardTable aTable从未被创建过。此外,这不是真正的BlackJack和CardTable是大约3000行代码... – peter9464