我被难住了。我一直在努力在这里和 NSubstitute 的文档中找到任何可行的答案来解决我的问题。
我正在尝试对服务方法进行单元测试,在该方法中,将对象添加到存储库后,我访问该对象的导航属性以执行其他工作。当单元测试到达代码中访问新对象的导航属性的位置时,我得到一个 NullReferenceException。我明白为什么我会收到异常,因为我没有告诉它它应该在模拟中返回什么,但我不知道如何告诉单元测试在访问导航属性时返回什么。这都是因为对象是在运行时创建的。
为简洁起见,我将只包括相关的方法部分。
// This is the method I am trying to unit test
public void SaveSelectedResultsMeasures(SelectResultsMeasuresViewModel model)
{
// validate the model
...
var cpf = this.repository.GetCareerPlanningFormByID(model.CareerPlanningFormID);
model.ResultsMeasuresSections.ToList().ForEach(resultsMeasuresSection =>
{
var selectedResultsMeasuresSection = cpf.SelectedResultsMeasuresSections
.Where(o => o.ResultsMeasuresSectionID == resultsMeasuresSection.SectionID)
.ToMaybe();
if (resultsMeasuresSection.IsSelected)
{
if (!selectedResultsMeasuresSection.HasValue)
{
// here is where the new object is being created and added to the repository
cpf.SelectedResultsMeasuresSections.Add(new SelectedResultsMeasuresSection
{
CareerPlanningFormID = cpf.CareerPlanningFormID,
IsDeleted = false,
ResultsMeasuresSectionID = resultsMeasuresSection.SectionID,
RepeatNumberOfOccurrences = resultsMeasuresSection.RepeatNumberOfOccurrences ?? 1
});
this.repository.Commit();
// now that the add has been committed, i get the object i just created
var newSelectedResultsMeasuresSection = cpf.SelectedResultsMeasuresSections
.Where(o => o.ResultsMeasuresSectionID == resultsMeasuresSection.SectionID)
.Single();
// here is where i access the navigation properties of that object
// this works just fine, just need to figure out how to unit test it so that i can continue to test the code inside the block
// when the unit test hits this point, this is where I get the NullReferenceException
newSelectedResultsMeasuresSection.ResultsMeasuresSection.ResultsMeasures.ToList().ForEach(rm =>
{
// code here...
}
}
}
}
}
// This is the unit test
[TestClass]
[ExcludeFromCodeCoverage]
public class CareerPlanningFormServiceTest
{
private readonly IPartnerPerformanceManagementConfiguration configuration;
private readonly IPartnerPerformanceManagementRepository repository;
private readonly IPrincipal currentUser;
private readonly CareerPlanningFormService target;
public CareerPlanningFormServiceTest()
{
this.configuration = Substitute.For<IPartnerPerformanceManagementConfiguration>();
this.repository = Substitute.For<IPartnerPerformanceManagementRepository>();
this.currentUser = Substitute.For<IPrincipal>();
this.target = new CareerPlanningFormService(
this.configuration,
this.repository,
this.currentUser);
}
/// <summary>
/// Tests the SaveSelectedResultsMeasures method.
/// Test success when adding a new SelectedResultsMeasuresSection
/// </summary>
[TestMethod]
[TestCategory("CareerPlanningFormService")]
[TestCategory("CareerPlanningFormService - SaveSelectedResultsMeasures")]
public void SaveSelectedResultsMeasures_Test_Success_Add_SelectedResultsMeasuresSection()
{
// Arrange
var model = new SelectResultsMeasuresViewModel
{
CareerPlanningFormID = 1,
ResultsMeasuresSections = new List<ResultsMeasuresSectionModel>
{
new ResultsMeasuresSectionModel
{
SectionID = 1,
IsRequired = false,
IsSelected = true,
ActionPlanLabels = new List<ActionPlanLabelModel>
{
new ActionPlanLabelModel
{
LabelID = 1,
IsSelected = true,
}
},
CanRepeat = false
},
new ResultsMeasuresSectionModel
{
SectionID = 2,
IsRequired = false,
IsSelected = true,
ActionPlanLabels = new List<ActionPlanLabelModel>(),
CanRepeat = true,
RepeatNumberOfOccurrences = 2
}
}
};
var cpf = new CareerPlanningForm
{
CareerPlanningFormID = 1,
SelectedResultsMeasuresSections = new EntitySet<SelectedResultsMeasuresSection>()
};
this.repository.GetCareerPlanningFormByID(model.CareerPlanningFormID).Returns(cpf);
// Act
this.target.SaveSelectedResultsMeasures(model);
// Assert
this.repository.Received(8).Commit();
Assert.AreEqual(2, cpf.SelectedResultsMeasuresSections.Count);
Assert.AreEqual(1, cpf.SelectedResultsMeasuresSections.Where(o => o.ResultsMeasuresSectionID == 1).Single().SelectedActionPlanLabels.Count());
Assert.AreEqual(1, cpf.SelectedResultsMeasuresSections.Where(o => o.ResultsMeasuresSectionID == 1).Single().ResultsMeasuresSectionOccurrences.Count());
Assert.AreEqual(0, cpf.SelectedResultsMeasuresSections.Where(o => o.ResultsMeasuresSectionID == 2).Single().SelectedActionPlanLabels.Count());
Assert.AreEqual(2, cpf.SelectedResultsMeasuresSections.Where(o => o.ResultsMeasuresSectionID == 2).Single().ResultsMeasuresSectionOccurrences.Count());
}
}
如果还有什么我可以提供的帮助,请告诉我。