2012-07-11 62 views
0

這似乎很簡單,但我似乎無法使其工作。我想單元測試我的控制器的分配動作。它需要一個IEnumerable(Of Integer)來表示要分配的所有對象的ID。如何測試控制器更新發布的內容

下面是我寫的代碼,我在Do語句中出錯(我從Ayende的博客http://ayende.com/blog/3397/rhino-mocks-3-5-a-feature-to-be-proud-of-seamless-do複製了代碼)。

<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 
+0

什麼是錯誤? – PatrickSteele 2012-07-11 19:29:01

+0

System.InvalidOperationException:委託返回值應該可以從HvacAudit – 2012-07-13 12:58:43

回答

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 
相關問題