Skip to content

(Always)Return doesn't seem to allow capturing locals by reference? #200

@CelticMinstrel

Description

@CelticMinstrel

The following minimal example demonstrates the issue:

#include <vector>
#include <string>

#include "catch.hpp"
#include "fakeit.hpp"

using namespace std;
using namespace fakeit;

struct IExample {
	virtual vector<string> getList() const = 0;
};

TEST_CASE("Mock capture by reference") {
	Mock<IExample> mock;
	vector<string> values;
	When(Method(mock, getList)).AlwaysReturn(ref(values));
	IExample& x = mock.get();
	CHECK(x.getList().size() == 0);
	values.push_back("Hello");
	values.push_back("World");
	REQUIRE(x.getList().size() == 2); // This assertion fails
	CHECK(x.getList()[0] == "Hello");
	CHECK(x.getList()[1] == "World");
}

I can see that (Always)Return has reference and non-reference overloads, maybe adding a reference_wrapper overload would help?

For now I've worked around it by directly using (Always)Do instead.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions