eranpeer / FakeIt

C++ mocking made easy. A simple yet very expressive, headers only library for c++ mocking.
MIT License
1.24k stars 170 forks source link

Mocking functions with move only parameters #57

Closed tnovotny closed 7 years ago

tnovotny commented 8 years ago

When trying to set up an expectation for a method that takes a non copyable argument such as a unique_ptr, I get the following message from the compiler (mscv 2015)

 attempting to reference a deleted function

Basically the class looks like this.

struct SomeInterface 
{
   virtual ~SomeInterface(){}   
   virtual void foo(std::unique_ptr<int> ) const = 0;
};

and the following code is enough to trigger the error.

Mock<SomeInterface> mock
Verify( Method(mock, foo) );

As passing unique_ptr by value is the preferred method for passing "sink" values, this is not that uncommon.

dgel commented 8 years ago

I'm interested in this as well. It seems FakeIt is storing the parameters of a call in order to find a matcher for it to return the correct value. It seems to be keeping the copies for later as well though, I'm not sure why. I'm using move-only types quite a bit, I'd like to use FakeIt, but it seems like it won't be possible..

ghost commented 7 years ago

The issue is unfortunately still not fixed for me. I really hope this can be fixed as FakeIt is really great and I'd love to use it.

The following code triggers an error:

class Testclass {
public:
    Testclass() {};
    virtual int* foo(std::unique_ptr<int> i) {
        i_ = std::move(i);
        return i_.get();
    };

protected:
    std::unique_ptr<int> i_;
};

Mock<Testclass> mock;
When(Method(mock, foo)).AlwaysReturn(nullptr);

Error message is:

C2665: 'std::tuple<std::unique_ptr<int,std::default_delete<_Ty>>>::tuple': none of the 2 overloads could convert all the argument types .....\fakeit.hpp 350

eranpeer commented 7 years ago

All the thanks to @dgel who submitted the fix.