我是C#的新手并且正在尝试学习单元测试。我有一个单元测试,它设置一个猜谜游戏的猜测列表并将它们存储在一个会话中。但是,单元测试失败了。
我的单元测试:
[TestMethod]
public void StartNewGameCausesANewTargetInTheGuessingGameGameStoredInSessionState()
{
//Arrange
var theGame = new GuessingGame();
List<int> originalTarget = new List<int>(theGame.Target);
var theContext = new FakeHttpContext();
var theKey = "GameState";
theContext.Session.Add(theKey, theGame);
var controller = new Exercise09Controller();
var request = new System.Web.Routing.RequestContext(theContext, new System.Web.Routing.RouteData());
controller.ControllerContext = new System.Web.Mvc.ControllerContext(request, controller);
//Act
controller.StartNewGame();
//Assert
Assert.IsInstanceOfType(controller.Session[theKey], typeof(GuessingGame));
CollectionAssert.AreNotEqual(originalTarget, theGame.Target);
}
我的班级:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Prigmore2013_01.Models
{
public class GuessingGame
{
private Random _random;
public GuessingGame()
{
this.Guesses = new List<Guess>();
this.Target = new List<int>() { 1, 2, 3 };
this._random = new Random();
}
public List<int> Target { get; set; }
public List<Guess> Guesses { get; set; }
public List<Guess> ShowGuessesMade()
{
return Guesses;
}
public void NewGame()
{
this.Target.Clear();
var count = 4;
for (var i = 1; i < count; i++)
{
var swap = _random.Next(1, 9);
if (!this.Target.Contains(swap))
{
this.Target.Add(swap);
}
// need to create a new random in generator
}
}
public void GuessTheHiddenDigits(List<int> guesses)
{
Guess g = new Guess() { Digits = guesses };
//compare the lists
var list = this.Target;
var list2 = g.Digits;
for (int i = 0; i < list.Count; i++)
{
if (list[i] == list2[i])
{
g.RightDigitRightPosition++;
}
}
//Now calculate how many digits in guesses are just plain wrong
List<int> result = g.Digits.Except(this.Target).ToList();
g.RightDigitWrongPosition = g.Digits.Count - result.Count - g.RightDigitRightPosition;
//handle duplicates
if (list.Count != list2.Distinct().Count())
{
// set thet right digit wrong position
for (int i = 0; i < list2.Distinct().Count(); i++ )
{
g.RightDigitWrongPosition = i;
}
}
this.Guesses.Add(g);
}
}
}
在我的控制器中,我尝试在Target
中设置StartNewGame()
我正确地执行此操作:
public RedirectToRouteResult StartNewGame()
{
var count = 4;
var random = new Random();
var result = new List<int>();
for (var i = 0; i < count; i++)
{
var swap = random.Next(1, 9);
if (!result.Contains(swap))
{
result.Add(swap);
}
// need to create a new random in generator
}
this.Session["GameState"] = new GuessingGame() { Target = result };
return RedirectToRoute(new
{
controller = "Index",
action = "Index"
});
}
我担心这个问题是在我的GuessingGame构造函数中引起的,因为它将目标设置为List<int>() {1,2,3}
,然后不会被覆盖。如何创建不同整数的新列表,然后将它们设置为我的会话?
答案 0 :(得分:0)
问题在于,在您的单元测试中,您使用的是theGame
,一个局部变量,供您进行比较。你应该改写你的第二个断言:
var sessionGame = controller.Session[theKey] as GuessingGame;
CollectionAssert.AreNotEqual(originalTarget, sessionGame.Target);
您的控制器未使用theGame
,它正在创建自己的GuessingGame
实例并相应地设置会话变量。您的第一个断言显示您理解这一点,但您只是缺少对控制器正在使用的类的实例的理解。
我相信你意识到,随着你的随机数生成,2 List集合的内容仍然可能是相同的。