问题出在底部,首先是一个背景:
我有两个嵌套工厂,比方说Team
和Player
。假设有一个商业规则是必须创建5个玩家编号为0并在团队创建时命名为“Bot”(并添加到该团队中)。
我有两家工厂:
public class PlayerFactory : IPlayerFactory
{
public IPlayer Create(int number, string name)
{
return new Player(number, name);
}
}
public class TeamFactory : ITeamFactory
{
private IPlayerFactory PlayerFactory { get; }
public TeamFactory(IPlayerFactory playerFactory)
{
PlayerFactory = playerFactory;
}
public ITeam Create(int height, int width)
{
// ... do some operations to create players
List<IPlayer> players = ...
return new Team(players);
}
}
到目前为止,这么好。现在..我想创建测试(好的,技术上我先创建测试):
[TestFixture]
public class PlayerFactoryTests
{
[Test]
public void CreatePlayerNumberTest()
{
IPlayerFactory playerFactory = new PlayerFactory();
IPlayer player = playerFactory.Create(2, "");
Assert.AreEqual(2, player.Number);
}
[Test]
public void CreatePlayerNameTest()
{
IPlayerFactory playerFactory = new PlayerFactory();
IPlayer player = playerFactory.Create(2, "John");
Assert.AreEqual("John", player.Name);
}
}
[TestFixture]
public class TeamFactoryTests
{
[Test]
public void CreateTeamTest()
{
IPlayerFactory playerFactory = new PlayerFactory();
ITeamFactory teamFactory = new TeamFactory(playerFactory);
ITeam team = teamFactory.Create();
}
}
问题是:
我应该在IPlayerFactory
测试中模拟CreateTeamTest
吗?
如果是的话,我该怎么做?
编辑:
public class Player : IPlayer
{
public int PlayerId { get; set; }
public string Name { get; set; }
public Player(int num)
{
PlayerId = num;
Name = "";
}
}
public ITeam Create(int number)
{
IPlayer[] players = new IPlayer[number];
for (int i = 0; i < players.GetLength(0); i++)
{
players[i] = PlayerFactory.Create(i);
}
return new Team(players);
}