测试期间不提供包含

时间:2015-12-15 14:47:19

标签: c# linq-to-entities

我的MVC控制器中有一段代码在运行时工作正常,但在测试失败期间。我用include来调用上下文。在运行时,我可以检查thisCall并看到thisCall.CallForContentCustomForm不为空。但是在测试期间调试它是空的。

我的模特:

public partial class CustomForm : BaseEntity
{

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid CustomFormId { get; set; }
    //...
}

public class CallForContent : BaseEntity
{
    [Key, ForeignKey("CallForContentResource"), DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid CallForContentId { get; set; }

    //...
    public Guid CallForContentFormId { get; set; }

    [ForeignKey("CallForContentFormId")]
    public CustomForm CallForContentCustomForm { get; set; }
}

在我的控制器中:

public partial class ProjectController : BaseController
{
    private IMkpContext db;

    #region "Constructor"

    public ProjectController()
    {
        db = new MkpContext();
    }

    public ProjectController(IMkpContext dbContext)
    {
        db = dbContext;
    }

    #endregion

    public virtual ActionResult callforcontentedit(Guid? id)
    {
        var thisCall =
            db.CallForContent
                .Include(cfc => cfc.CallForContentCustomForm)
                .Include(cfc => cfc.Project)
                .FirstOrDefault(cfc => cfc.CallForContentId == id);
        // At this point during run-time thisCall.CallForContentCustomForm has a value,
        // but is null during testing

        // This row does return the expected result during testing.
        var customForms = db.CustomForms.Where(c => c.IsActive && c.CustomFormId == thisCall.CallForContentFormId);

        if (thisCall == null) return RedirectToAction("Index", "Submission");

        var viewModel = Mapper.Map<CallForContentViewModel>(thisCall);
        // ...
        ViewBag.Title = viewModel.CallForContentCustomForm.FormName; // Fails here during tests
    }
}

在我的测试设置中,我确实将数据添加到CustomForm表的FakeDbContext中。

        var newCallForContentForm = FakeCallForContentForm(newProfile.ProfileId);
        fakeDb.CustomForms.Add(newCallForContentForm);
        //...

        var newCallForContentProposalForm = FakeCallForContentProposalForm(newProfile.ProfileId);
        fakeDb.CustomForms.Add(newCallForContentProposalForm);

我的测试:

[TestMethod]
public void Project_CallForContent_Test()
{
    // Arrange
    var fakeDb = TestFakes.SetupFakeDbContext();
    var controller = TestFakes.ProjectController(fakeDb);

    // Act
    var result = controller.callforcontentedit(fakeDb.CallForContent.FirstOrDefault().CallForContentId) as ViewResult;

    // Assert
    Assert.IsNotNull(result);
}

以下是我的上下文:

public class FakeMkpContext : AuditDbContext, IMkpContext
{
    #region Constructors

    static FakeMkpContext()
    {
        Database.SetInitializer<FakeMkpContext>(null);

    }

    public FakeMkpContext() : base("Name=FakeMkpContext")
    {
        this.Profile = new FakeProfileSet();
    }

    #endregion

    #region Property (Dbset)

    public IDbSet<Address> Address { get; set; }
    public IDbSet<Keyword> Keyword { get; set; }
    public IDbSet<CustomForm> CustomForms { get; set; }
    public IDbSet<SubmittedForm> SubmittedForms { get; set; }
    public IDbSet<SubmittedFormData> SubmittedFormData { get; set; }

    public IDbSet<Project> Project { get; set; }
    public IDbSet<ProjectType> ProjectType { get; set; }

    public IDbSet<CustomFieldGroup> CustomFieldGroup { get; set; }
    public IDbSet<CustomFormGroup> CustomFormGroup { get; set; }
    public IDbSet<CallForContent> CallForContent { get; set; }
    #endregion
    //...
}

我没有将Moq用于我测试的实体框架部分。

0 个答案:

没有答案