Skip to content

Instantly share code, notes, and snippets.

@choeehb
Created January 8, 2021 09:24
Show Gist options
  • Save choeehb/b08460f6aa5f98c4edb2e7ee64c25640 to your computer and use it in GitHub Desktop.
Save choeehb/b08460f6aa5f98c4edb2e7ee64c25640 to your computer and use it in GitHub Desktop.
아웃게임 코드 예시
using System;
using NSubstitute;
using NUnit.Framework;
using POP.Ingame.GameLogic;
using UniRx;
namespace TestTest
{
public class UserModel
{
public IntRxProp Gold = new IntRxProp();
public IntRxProp Piece = new IntRxProp();
}
public class CharacterUpgradeEntity
{
public virtual int GetPieceRequired() => 100;
public virtual int GetGoldRequired() => 100;
}
public class CharacterAPI
{
public virtual bool Upgrade(CharacterType type)
{
// 서버로 대충 int로 바꿔서 보냄.
return false;
}
}
public class CharacterUpgradeModel
{
public IObservable<CharacterType> OnUpgrade => onUpgrade;
private readonly Subject<CharacterType> onUpgrade = new Subject<CharacterType>();
private readonly UserModel userModel;
private readonly CharacterUpgradeEntity entity;
private readonly CharacterAPI api;
public CharacterUpgradeModel(
UserModel userModel,
CharacterUpgradeEntity entity,
CharacterAPI api
)
{
this.userModel = userModel;
this.entity = entity;
this.api = api;
}
public void Upgrade(CharacterType type)
{
bool enoughGold = entity.GetGoldRequired() <= userModel.Gold.Value;
bool enoughPiece = entity.GetPieceRequired() <= userModel.Piece.Value;
bool canUpgrade = enoughGold && enoughPiece;
if (!canUpgrade)
return;
var success = api.Upgrade(type);
if (success)
onUpgrade.OnNext(type);
}
}
public class CharacterUpgradeTest
{
UserModel userModel;
CharacterUpgradeEntity entity;
CharacterAPI api;
CharacterUpgradeModel model;
[SetUp]
public void SetUp()
{
userModel = new UserModel();
entity = Substitute.For<CharacterUpgradeEntity>();
entity.GetGoldRequired().Returns(1000); // 골드 요구량
entity.GetPieceRequired().Returns(2000); // 조각 요구량
api = Substitute.For<CharacterAPI>();
model = new CharacterUpgradeModel(userModel, entity, api);
}
[TestCase(999, 1999, false)]
[TestCase(1000, 1999, false)]
[TestCase(999, 2000, false)]
[TestCase(1000, 2000, true)]
public void 골드랑조각이충분하면_api에요청이간다(int gold, int piece, bool expected)
{
골드갯수(gold);
조각갯수(piece);
model.Upgrade(CharacterType.Red);
if (expected)
api.Received().Upgrade(CharacterType.Red);
else
api.DidNotReceive().Upgrade(CharacterType.Red);
}
private void 골드갯수(int amount)
{
userModel.Gold.Value = amount;
}
private void 조각갯수(int amount)
{
userModel.Piece.Value = amount;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment