模拟在运行时使用NSubstitute创建的对象的LINQ-to-SQL导航属性

时间:2012-12-04 16:40:25

标签: .net linq-to-sql mocking nsubstitute

我在这里难过。我一直在努力在这里和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());
    }
}

如果还有其他任何可以提供的帮助,请告诉我。

1 个答案:

答案 0 :(得分:0)

您似乎需要将ResultsMeasures添加到您在测试中返回的ResultsMeasuresSections吗?

在你走这条路之前,看起来你正在为simulate the behaviour of LINQ-to-SQL做很多工作。我建议删除测试代码所用特定机制的单元测试,并切换到命中实际或内存数据库的测试,以确保您的代码具有您想要的行为。例如,如果您已保存CPF的结果度量,请确保检索该CPF具有您需要的信息。

测试诸如.Received(8).Commit()之类的事情的缺点是,您正在测试您如何知道/期望其他系统的行为,而不是代码是否实际上按要求行事。在测试时,我觉得后一点对我们来说是最有价值的。