…
namespace TestProject1
{
[TestFixture]
public class InvoiceNumberApplyTests
{
[OneTimeSetUp]
public void OneTimeInit()
{
}
[SetUp]
public void EveryTestInit()
{
//controller呼叫
controller = new InvoiceNumberApplyController();
//Validation測試
validationResults = new List<ValidationResult>();
}
[TearDown]
public void Cleanup()
{
}
//收到為ViewResult的測試案例
[Test, Order(1)] //執行順序
public void TransferOrganization_BusinessIDNull_Fail()
{
var result = controller.TransferOrganization(null);
Assert.IsTrue(result is ViewResult);
ViewResult viewResult = result as ViewResult;
Assert.IsTrue(viewResult.ViewName.Contains("Error"));
}
//測試landing的頁面網址和預期是否符合
[Test, Order(8)]
public void Url_Security_NoPrivilegeShouldGoLoginPage_OK()
{
driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://localhost:2598/InvoiceNumberApply/Update");
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10000);
Assert.True(driver.Url.Contains("/Account/Login"), url+ " 回傳網頁與預期(/Account/Login)不符");
driver.Close();
}
//收到為memoryStream(application/zip)的測試案例
[Test, Order(15)]
public void Controller_SetAll_OK()
{
var result = controller.SetAll(viewModel.Apply.BusinessID);
Assert.IsTrue(result is FileContentResult, "SetAll的result應該是FileContentResult");
FileContentResult view = (FileContentResult)result;
}
//實作ValidationContext測試Custom Validator範例
//直接呼叫Controller不會觸發Validator檢查
[Test, Order(10)]
public void CustomedValidator_InvalidAppointDateDuration_InvoiceNumberApply()
{
//Model validation, as part of the model binding process,gets invoked outside of the controller.
//That means that you cannot really test it inside of a controller unit test.
testApply.ApplyByAppointDateFrom = DateTime.Parse("2023-06-28");
testApply.ApplyByAppointDateTo = DateTime.Parse("2013-06-28");
var isValid = false;
(isValid, validationResults) = GetTryValidateObject<InvoiceNumberApply>(testApply);
Assert.That(isValid, Is.False, testApply.ApplyByAppointDateFrom.ToString() + "應要小於" + testApply.ApplyByAppointDateTo.ToString());
Assert.That(validationResults.FirstOrDefault().ErrorMessage.Contains("委任迄日不應小於委任起日"));
}
public (bool, List<ValidationResult>) GetTryValidateObject<T>(T target)
{
ValidationContext validationContext = new ValidationContext(target);
bool isValid
= Validator.TryValidateObject(target, validationContext, validationResults, true);
return (isValid, validationResults);
}
//多組TestCase setting以物件方式設定測試範例
[Test, Order(13)]
[TestCaseSource(typeof(TestCaseStrings), nameof(TestCaseStrings.營業人名稱Cases))]
[TestCaseSource(typeof(TestCaseStrings), nameof(TestCaseStrings.AddressCases))]
public void Validator_Regex(string pattern, string input, bool result, string message)
{
bool match = Regex.IsMatch(input, pattern);
Assert.That(match==result, "pattern="+pattern + " input=" + input + " "+ message);
}
public class TestCaseStrings
{
static string 營業人名稱Pattern = @"^[\u4E00-\u9FFF-,]{0,50}$";
public static object[] 營業人名稱Cases =
{
new object[] { 營業人名稱Pattern, "英屬維京群島商利時有限公司台灣分公司永春門市部英屬維京群島商利時有限公司台灣分公司永春門市部門市部", true, "正常" },
new object[] { 營業人名稱Pattern, "英屬維京群島商利時有限公司台灣分公司永春門市部英屬維京群島商利時有限公司台灣分公司永春門市部門市部部部", false, "超過50字太長" },
new object[] { 營業人名稱Pattern, "英屬維京群Island商利時有限公司", false, "不該有英文"},
new object[] { 營業人名稱Pattern, "123商利時有限公司", false, "不該有數字" }
};
static string AddressPattern = @"^[\u4E00-\u9FFFA-Za-z0-9-,]{0,40}$";
public static object[] AddressCases =
{
new object[] { AddressPattern, "台中市大雅區中清路三段1207號", false, "數字不收全型" },
new object[] { AddressPattern, "台北市萬華區中華路二段48巷11號1樓", true, "正常" },
new object[] { AddressPattern, "高雄市苓雅區成功一路232號17-1號37號碼頭", true, "正常"},
new object[] { AddressPattern, "高雄市大寮區鳳屏二路65-1號", true, "正常" },
new object[] { AddressPattern, "高雄市大abc號", true, "正常" }
};
}
}
}