How can I have a gmock object return a fixed std::forward_list

39 Views Asked by At

I'm trying to write this test:

TEST(AccountServiceShould, print_a_statement_containing_all_transactions) {
    auto transactionRepository = new TransactionRepositoryMock;
    std::forward_list<model::Transaction *> transactionList;
    auto statementPrinter = new StatementPrinterMock;

    transactionList.assign({transaction("22/12/2019", 1000)});

    ON_CALL(
            *transactionRepository,
            all()
    )
            .WillByDefault(Return(transactionList));

    Clock *myClock = new Clock;

    auto accountService = new AccountService(transactionRepository, myClock);

    EXPECT_CALL(*statementPrinter, print(Eq(transactionList)));

    accountService->printStatement();

    delete accountService;
    delete transactionRepository;
    delete myClock;
    delete statementPrinter;
}

And I'm getting an error on compile time:

No viable conversion from 'internal::ReturnAction<forward_list<Transaction *, allocator<Transaction *>>>' to 'const Action<std::forward_list<model::Transaction, std::allocator<model::Transaction>> ()>' candidate template ignored: requirement 'internal::disjunction<std::is_constructible<std::function<std::forward_list<model::Transaction, std::allocator<model::Transaction>> ()>, testing::internal::ReturnAction<std::forward_list<model::T... candidate template ignored: could not match 'Action' against 'ReturnAction' explicit constructor is not a candidate candidate template ignored: could not match 'OnceAction' against 'Action' candidate template ignored: requirement 'conjunction<testing::internal::negation<std::is_same<void, std::forward_list<model::Transaction, std::allocator<model::Transaction>>>>, testing::internal::negation<std::is_reference<std::forward_... passing argument to parameter 'action' here

I don't understand where the problem is. I tried to change the type of transactionList, use a dynamic object and even created my own MATCHER_P but I can't figure this one out.

I'm not an expert in C++, pretty much the opposite.

Thanks!

1

There are 1 best solutions below

1
pptaszni On

It should be a comment, but a bit too many characters.

No viable conversion from

forward_list<Transaction *, allocator<Transaction *>>

to

std::forward_list<model::Transaction, std::allocator<model::Transaction>>

So, I'm 90% (because ofc you didn't include relevant info in the question) sure you have a typo, because your mock returns std::forward_list<Transaction> and you try to return std::forward_list<Transation*>.

This compiles without problems:

struct Transaction {};
class TransactionRepositoryMock {
public:
    MOCK_METHOD(std::forward_list<Transaction*>, all, ());
};
TEST(AccountServiceShould, print_a_statement_containing_all_transactions) {
    auto transactionRepository = new TransactionRepositoryMock;
    std::forward_list<Transaction*> transactionList;
    transactionList.assign({new Transaction});
    ON_CALL(*transactionRepository, all()).WillByDefault(Return(transactionList));
}