Skip to content

Pass auto lambda function to invoke() #63

@brendan-simon-indt

Description

@brendan-simon-indt

How can I pass a C++ lambda function to invoke() as a mock implementation of the function foo()?

I want something like this in my test.

    //! lambda function to implement mocked alternative function
    auto mock_foo = [](bar_t *bar, int len)
    {
        bar->a = 10;
        bar->b = 11;
        bar->x = len;
    };
    MOCKER(foo).stubs().will(invoke(mock_foo));

I couldn't get that to work with auto, but it does work if explicitly declare a function pointer (but it's more unnecessary boiler plate)

    //! lambda function to implement mocked alternative function
    void (*mock_foo)(bar_t *bar, int len) = [](bar_t *bar, int len)
    {
        bar->a = 10;
        bar->b = 11;
        bar->x = len;
    };
    MOCKER(foo).stubs().will(invoke(mock_foo));

How can I avoid the unnecessary boiler plate and use auto with my lamda function?
I should also be able to use an anonymous lambda function directly in invoke().

    //! lambda function to implement mocked alternative function
    MOCKER(foo).stubs().will(invoke(
      [](bar_t *bar, int len)
          {
              bar->a = 10;
              bar->b = 11;
              bar->x = len;
          };
    ));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions