我是编写单元测试的新手,我尝试了一些有关Nlog和NSubstitute的教程。 我正在尝试为此方法编写单元测试,并且发生期望时的结果是一个新的“ CalendarDaysItem”:
public CalendarDaysItem GetFullDate(int year, int month, int day, string culture = "pt-PT")
{
CalendarDaysItem model;
try
{
DateTime date = new DateTime(year, month, day);
string dayExntension = date.ToString("dddd", CultureInfo.CreateSpecificCulture(culture)).ToUppercaseFirst();
string monthExntension = date.ToString("MMMM", CultureInfo.CreateSpecificCulture(culture)).ToUppercaseFirst();
model = new CalendarDaysItem(year, month, day)
{
FullDate = dayExntension + ", " + day + " " + monthExntension + ", " + year,
};
}
catch (Exception ex)
{
model = new CalendarDaysItem();
LoggerManager.Log(LoggerManager.LoggerType.MAIN, LogLevel.Error, null, null, ex.Message);
}
return model;
}
测试用例
[Test]
[TestCase(2018,8,37)]
public void GetFullDate_GetFullStringDateOutRange_ReturnEmptyDate(int year, int month, int day)
{
CalendarDaysItem calendar = new CalendarDaysItem();
var result = _substitute.GetFullDate(year, month, day).Returns(new CalendarDaysItem());
Assert.AreEqual(calendar, result);
}
我已经尝试使用Recived()。 当我使用此日历对象时,它为null,并且断言失败。 有推荐的初学者教程来测试对象吗?
请注意,我编写了此测试并通过:
[Test]
[TestCase(2018,8,27, "Segunda-feira, 27 Agosto, 2018")]
public void GetFullDate_GetFullStringDate_ReturnFullDate(int year, int month, int day, string expectedResult)
{
CalendarDaysItem calendar = new CalendarDaysItem(year, month, day)
{
FullDate = expectedResult,
};
var result = _substitute.GetFullDate(year, month, day).Returns(calendar);
Assert.AreEqual(calendar.FullDate, expectedResult);
}