C# 控制台项目 - NUnit 检查验证消息
C# Console Project - NUnit to check validation message
我有这个工作方法,我想为它编写 NUnit 测试用例方法。这是一个控制台项目,这意味着错误消息将通过 Console.WriteLine 方法打印,我在 Utility class 中使用 PrintMessage 方法来执行此操作。第二个参数是用布尔值控制Console.Color(红色表示错误信息)。
public void PlaceDeposit(BankAccount account, decimal _transaction_amt)
{
if (_transaction_amt <= 0)
Utility.PrintMessage("Amount needs to be more than zero. Try again.", false);
else if (_transaction_amt % 10 != 0)
Utility.PrintMessage($"Key in the deposit amount only with multiply of 10. Try again.", false);
else if (!PreviewBankNotesCount(_transaction_amt))
Utility.PrintMessage($"You have cancelled your action.", false);
else
{
// Bind transaction_amt to Transaction object
// Add transaction record - Start
var transaction = new Transaction()
{
AccountID = account.Id,
BankAccountNoTo = account.AccountNumber,
TransactionType = TransactionType.Deposit,
TransactionAmount = _transaction_amt,
TransactionDate = DateTime.Now
};
repoTransaction.InsertTransaction(transaction);
// Add transaction record - End
account.Balance = account.Balance + _transaction_amt;
ctx.SaveChanges();
Utility.PrintMessage($"You have successfully deposited {Utility.FormatAmount(_transaction_amt)}", true);
}
}
我创建了另一个 NUnit 测试项目来测试上面的方法,但我遇到了断言。我应该将上述方法修改为 return 字符串(方法输出消息)以创建 NUnit 测试用例,还是应该着手更改我的原始方法?
[TestFixture]
public class TestATMCustomer
{
[TestCase]
public void PlaceDeposit()
{
// Arrange
BankAccount bankAccount = new BankAccount() {
FullName = "John", AccountNumber=333111, CardNumber = 123, PinCode = 111111, Balance = 2300.00m, isLocked = false
};
decimal transactionAmount = 120;
var atmCustomer = new MeybankATM();
// Act
// Act and Assert
Assert.AreEqual(atmCustomer.PlaceDeposit(bankAccount, transactionAmount));
}
}
已更新测试用例,但 MeybankATM 构造函数中存在错误
// Arrange - Start
var mock = new MockMessagePrinter();
MeybankATM atmCustomer = new MeybankATM(new RepoBankAccount(), new RepoTransaction(), mock);
BankAccount bankAccount = new BankAccount()
{
FullName = "John",
AccountNumber = 333111,
CardNumber = 123,
PinCode = 111111,
Balance = 2000.00m,
isLocked = false
};
decimal transactionAmount = 0;
// Arrange - End
// Act
atmCustomer.PlaceDeposit(bankAccount, transactionAmount);
// Assert
var expectedMessage = "Amount needs to be more than zero. Try again.";
Assert.AreEqual(expectedMessage, mock.Message);
class Program
{
static void Main(string[] args)
{
var mock = new MockMessagePrinter();
ATMCustomer atmCustomer = new ATMCustomer(mock, new RepoTransaction());
atmCustomer.PlaceDeposit(new BankAccount(), 0);
Console.WriteLine(mock.Message == "Amount needs to be more than zero. Try again.");
Console.ReadLine();
}
}
public class ATMCustomer
{
private readonly IMessagePrinter _msgPrinter;
private readonly IRepoTransaction _repoTransaction;
public ATMCustomer(IMessagePrinter msgPrinter, IRepoTransaction repoTransaction)
{
_msgPrinter = msgPrinter;
_repoTransaction = repoTransaction;
}
public void PlaceDeposit(BankAccount account, decimal _transaction_amt)
{
if (_transaction_amt <= 0)
_msgPrinter.PrintMessage("Amount needs to be more than zero. Try again.", false);
else if (_transaction_amt % 10 != 0)
_msgPrinter.PrintMessage($"Key in the deposit amount only with multiply of 10. Try again.", false);
else if (!PreviewBankNotesCount(_transaction_amt))
_msgPrinter.PrintMessage($"You have cancelled your action.", false);
else
{
// Bind transaction_amt to Transaction object
// Add transaction record - Start
var transaction = new Transaction()
{
AccountID = account.Id,
BankAccountNoTo = account.AccountNumber,
TransactionType = TransactionType.Deposit,
TransactionAmount = _transaction_amt,
TransactionDate = DateTime.Now
};
_repoTransaction.InsertTransaction(transaction);
// Add transaction record - End
account.Balance = account.Balance + _transaction_amt;
//ctx.SaveChanges();
//_msgPrinter.PrintMessage($"You have successfully deposited {Utility.FormatAmount(_transaction_amt)}", true);
}
}
private bool PreviewBankNotesCount(decimal transactionAmt)
{
throw new NotImplementedException();
}
}
public class MockMessagePrinter : IMessagePrinter
{
private string _message;
public string Message => _message;
public void PrintMessage(string message, bool idontKnow)
{
_message = message;
}
}
public interface IRepoTransaction
{
void InsertTransaction(Transaction transaction);
}
public class RepoTransaction : IRepoTransaction
{
public void InsertTransaction(Transaction transaction)
{
throw new NotImplementedException();
}
}
public interface IMessagePrinter
{
void PrintMessage(string message, bool iDontKnow);
}
public class BankAccount
{
public decimal Balance { get; set; }
public string AccountNumber { get; set; }
public int Id { get; set; }
}
public class Transaction
{
public int AccountID { get; set; }
public string BankAccountNoTo { get; set; }
public TransactionType TransactionType { get; set; }
public decimal TransactionAmount { get; set; }
public DateTime TransactionDate { get; set; }
}
public enum TransactionType
{
Deposit
}
我已经复制了你的代码并修改了一些:
- 我重构了您的代码以使用 IMessagePrinter 而不是 Utility,这样我就可以注入一个模拟对象来检查传递给 PrintMessage 方法的内容
- 我没有使用 NUnit - 我只是使用控制台项目作为示例
- 我假设使用了数据类型/类,但这并不重要
希望对您有所帮助
为 NUnit 编辑:
public class TestAtmCustomer
{
[Test]
public void Should_ShowZeroErrorMessage_OnPlaceDeposit_When_AmountIsZero()
{
var mock = new MockMessagePrinter();
ATMCustomer atmCustomer = new ATMCustomer(mock, new RepoTransaction());
atmCustomer.PlaceDeposit(new BankAccount(), 0);
var expectedMessage = "Amount needs to be more than zero. Try again.";
Assert.AreEqual(expectedMessage, mock.Message);
}
}
我有这个工作方法,我想为它编写 NUnit 测试用例方法。这是一个控制台项目,这意味着错误消息将通过 Console.WriteLine 方法打印,我在 Utility class 中使用 PrintMessage 方法来执行此操作。第二个参数是用布尔值控制Console.Color(红色表示错误信息)。
public void PlaceDeposit(BankAccount account, decimal _transaction_amt)
{
if (_transaction_amt <= 0)
Utility.PrintMessage("Amount needs to be more than zero. Try again.", false);
else if (_transaction_amt % 10 != 0)
Utility.PrintMessage($"Key in the deposit amount only with multiply of 10. Try again.", false);
else if (!PreviewBankNotesCount(_transaction_amt))
Utility.PrintMessage($"You have cancelled your action.", false);
else
{
// Bind transaction_amt to Transaction object
// Add transaction record - Start
var transaction = new Transaction()
{
AccountID = account.Id,
BankAccountNoTo = account.AccountNumber,
TransactionType = TransactionType.Deposit,
TransactionAmount = _transaction_amt,
TransactionDate = DateTime.Now
};
repoTransaction.InsertTransaction(transaction);
// Add transaction record - End
account.Balance = account.Balance + _transaction_amt;
ctx.SaveChanges();
Utility.PrintMessage($"You have successfully deposited {Utility.FormatAmount(_transaction_amt)}", true);
}
}
我创建了另一个 NUnit 测试项目来测试上面的方法,但我遇到了断言。我应该将上述方法修改为 return 字符串(方法输出消息)以创建 NUnit 测试用例,还是应该着手更改我的原始方法?
[TestFixture]
public class TestATMCustomer
{
[TestCase]
public void PlaceDeposit()
{
// Arrange
BankAccount bankAccount = new BankAccount() {
FullName = "John", AccountNumber=333111, CardNumber = 123, PinCode = 111111, Balance = 2300.00m, isLocked = false
};
decimal transactionAmount = 120;
var atmCustomer = new MeybankATM();
// Act
// Act and Assert
Assert.AreEqual(atmCustomer.PlaceDeposit(bankAccount, transactionAmount));
}
}
已更新测试用例,但 MeybankATM 构造函数中存在错误
// Arrange - Start
var mock = new MockMessagePrinter();
MeybankATM atmCustomer = new MeybankATM(new RepoBankAccount(), new RepoTransaction(), mock);
BankAccount bankAccount = new BankAccount()
{
FullName = "John",
AccountNumber = 333111,
CardNumber = 123,
PinCode = 111111,
Balance = 2000.00m,
isLocked = false
};
decimal transactionAmount = 0;
// Arrange - End
// Act
atmCustomer.PlaceDeposit(bankAccount, transactionAmount);
// Assert
var expectedMessage = "Amount needs to be more than zero. Try again.";
Assert.AreEqual(expectedMessage, mock.Message);
class Program
{
static void Main(string[] args)
{
var mock = new MockMessagePrinter();
ATMCustomer atmCustomer = new ATMCustomer(mock, new RepoTransaction());
atmCustomer.PlaceDeposit(new BankAccount(), 0);
Console.WriteLine(mock.Message == "Amount needs to be more than zero. Try again.");
Console.ReadLine();
}
}
public class ATMCustomer
{
private readonly IMessagePrinter _msgPrinter;
private readonly IRepoTransaction _repoTransaction;
public ATMCustomer(IMessagePrinter msgPrinter, IRepoTransaction repoTransaction)
{
_msgPrinter = msgPrinter;
_repoTransaction = repoTransaction;
}
public void PlaceDeposit(BankAccount account, decimal _transaction_amt)
{
if (_transaction_amt <= 0)
_msgPrinter.PrintMessage("Amount needs to be more than zero. Try again.", false);
else if (_transaction_amt % 10 != 0)
_msgPrinter.PrintMessage($"Key in the deposit amount only with multiply of 10. Try again.", false);
else if (!PreviewBankNotesCount(_transaction_amt))
_msgPrinter.PrintMessage($"You have cancelled your action.", false);
else
{
// Bind transaction_amt to Transaction object
// Add transaction record - Start
var transaction = new Transaction()
{
AccountID = account.Id,
BankAccountNoTo = account.AccountNumber,
TransactionType = TransactionType.Deposit,
TransactionAmount = _transaction_amt,
TransactionDate = DateTime.Now
};
_repoTransaction.InsertTransaction(transaction);
// Add transaction record - End
account.Balance = account.Balance + _transaction_amt;
//ctx.SaveChanges();
//_msgPrinter.PrintMessage($"You have successfully deposited {Utility.FormatAmount(_transaction_amt)}", true);
}
}
private bool PreviewBankNotesCount(decimal transactionAmt)
{
throw new NotImplementedException();
}
}
public class MockMessagePrinter : IMessagePrinter
{
private string _message;
public string Message => _message;
public void PrintMessage(string message, bool idontKnow)
{
_message = message;
}
}
public interface IRepoTransaction
{
void InsertTransaction(Transaction transaction);
}
public class RepoTransaction : IRepoTransaction
{
public void InsertTransaction(Transaction transaction)
{
throw new NotImplementedException();
}
}
public interface IMessagePrinter
{
void PrintMessage(string message, bool iDontKnow);
}
public class BankAccount
{
public decimal Balance { get; set; }
public string AccountNumber { get; set; }
public int Id { get; set; }
}
public class Transaction
{
public int AccountID { get; set; }
public string BankAccountNoTo { get; set; }
public TransactionType TransactionType { get; set; }
public decimal TransactionAmount { get; set; }
public DateTime TransactionDate { get; set; }
}
public enum TransactionType
{
Deposit
}
我已经复制了你的代码并修改了一些:
- 我重构了您的代码以使用 IMessagePrinter 而不是 Utility,这样我就可以注入一个模拟对象来检查传递给 PrintMessage 方法的内容
- 我没有使用 NUnit - 我只是使用控制台项目作为示例
- 我假设使用了数据类型/类,但这并不重要
希望对您有所帮助
为 NUnit 编辑:
public class TestAtmCustomer
{
[Test]
public void Should_ShowZeroErrorMessage_OnPlaceDeposit_When_AmountIsZero()
{
var mock = new MockMessagePrinter();
ATMCustomer atmCustomer = new ATMCustomer(mock, new RepoTransaction());
atmCustomer.PlaceDeposit(new BankAccount(), 0);
var expectedMessage = "Amount needs to be more than zero. Try again.";
Assert.AreEqual(expectedMessage, mock.Message);
}
}