0

这看起来很简单,但我似乎无法让它工作。我想对控制器的分配操作进行单元测试。它需要一个 IEnumerable(Of Integer) 来表示要分配的所有对象的 ID。

这是我编写的代码,Do 语句出现错误(我从 Ayende 的博客中复制了代码http://ayende.com/blog/3397/rhino-mocks-3-5-a-feature -为无缝做而感到自豪)。

    <Test()> _
    Public Sub Assign_Post_Should_Assign_All_Audits_Provided()
        Dim auditsToAssign As IEnumerable(Of HvacAudit) = HvacAuditsGenerator.GenerateAudits() _
                                                                .Unassigned()

        Dim auditIDs As IEnumerable(Of Integer) = auditsToAssign.Select(Function(audit, index) audit.HvacAuditID)

        Dim hvacAuditRepo As IHvacAuditRepository = MockRepository.GenerateMock(Of IHvacAuditRepository)()
        hvacAuditRepo.Stub(Sub(repo) repo.GetAuditByID(1)) _
            .Do(Function(invocation) invocation.ReturnValue = auditsToAssign.Single(Function(audit) audit.HvacAuditID = invocation.Arguments(0)))

        Dim controller As New HvacAuditController(hvacAuditRepo)
        Dim r As ViewResult = controller.Assign(auditIDs).AssertViewRendered()

        r.AssertAssignedAuditCount(auditsToAssign.Count)
        auditsToAssign.AssertAreAssigned()
        hvacAuditRepo.AssertWasCalled(Sub(h) h.SaveChanges())
    End Sub
4

1 回答 1

0

我可以通过更改代码以使用函数 lambda 而不是子 lambda 并直接返回值来解决此问题。我不确定 Ayende 的博客指的是什么。

<Test()> _
Public Sub Assign_Post_Should_Assign_All_Audits_Provided()
    Dim auditsToAssign As IEnumerable(Of HvacAudit) = HvacAuditsGenerator.GenerateAudits() _
                                                            .Unassigned()

    Dim auditIDs As IEnumerable(Of Integer) = auditsToAssign.Select(Function(audit, index) audit.HvacAuditID)

    Dim hvacAuditRepo As IHvacAuditRepository = MockRepository.GenerateMock(Of IHvacAuditRepository)()
    hvacAuditRepo.Stub(Sub(repo) repo.GetAuditByID(1)) _
        .Do(Function(auditID as Integer) Return auditsToAssign.Single(Function(audit) audit.HvacAuditID = auditID))

    Dim controller As New HvacAuditController(hvacAuditRepo)
    Dim r As ViewResult = controller.Assign(auditIDs).AssertViewRendered()

    r.AssertAssignedAuditCount(auditsToAssign.Count)
    auditsToAssign.AssertAreAssigned()
    hvacAuditRepo.AssertWasCalled(Sub(h) h.SaveChanges())
End Sub
于 2012-07-13T13:33:30.030 回答