【问题标题】:Using Lambda in Unit Test in VB.NET 2008 with Rhino.Mocks在 VB.NET 2008 的单元测试中使用 Lambda 和 Rhino.Mocks
【发布时间】:2010-10-25 13:58:36
【问题描述】:

我正在尝试创建一个类似于我在 C# 中完成的单元测试,但我在 vb 中的 lambdas 上苦苦挣扎。

基本上我试图模拟一个类,然后创建一个存根并返回。在 C# 中,我会做类似的事情;

MockedPersonRepository
     .Stub(x => x.Find(id))
     .Return(person)

但在 Visual Basic 中,我正在尝试做类似的事情,但无法计算出语法

   MockedPersonRepository.Stub(Function... argh!!!

任何关于如何重现上述内容的建议将不胜感激!

亲切的问候, 多姆

【问题讨论】:

    标签: .net vb.net unit-testing lambda rhino-mocks


    【解决方案1】:

    我通常展示的一个简单示例(因为我也是 VB 开发人员)如下所示:(由于 VB 中的一些奇怪原因,您需要将其拉出到另一个不返回任何内容的函数中)

      <TestMethod()> _
      Public Sub Should_Call_Into_Repository_For_GetAllUsers()
        Dim Repository As IUserRepository = MockRepository.GenerateStub(Of IUserRepository)()
        Dim Service As IUserService = New UserService(Repository)
    
        Service.GetAllUserCollection()
    
        Repository.AssertWasCalled(Function(x) Wrap_GetAllUserCollection(x))
      End Sub
    
    Function Wrap_GetAllUserCollection(ByVal Repository As IUserRepository) As Object
        Repository.GetAllUserCollection()
    
        Return Nothing
      End Function
    

    以上是基于交互的测试,以下可能更接近您在当前示例中寻找的内容

    Dim StubUserObject As New User(1, "9999", "jdoe", "John", "Doe", 1)
    
        UserService.Stub(Function(x) x.GetUserByID(1)).[Return](StubUserObject)
    

    【讨论】:

      【解决方案2】:

      这样的东西有用吗?

      MockedPersonRepository_
          .Stub(Function(x) x.Find(id))_
          .[Return](person)
      

      【讨论】:

        猜你喜欢
        • 2023-03-24
        • 1970-01-01
        • 2015-11-07
        • 2019-01-22
        • 2010-10-17
        • 2023-04-04
        • 2011-10-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多