如何让 gmock 对象返回固定的 std::forward_list

问题描述 投票:0回答:1

我正在尝试编写这个测试:

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;
}

我在编译时遇到错误:

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

我不明白问题出在哪里。我尝试更改

transactionList
的类型,使用动态对象,甚至创建了自己的 MATCHER_P,但我无法弄清楚这个。

我不是 C++ 专家,恰恰相反。

谢谢!

c++ std googlemock
1个回答
0
投票

应该是评论,但是字符太多了。

没有可行的转换

forward_list<Transaction *, allocator<Transaction *>>

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

所以,我90%(因为ofc你没有在问题中包含相关信息)确定你有一个拼写错误,因为你的模拟返回

std::forward_list<Transaction>
并且你尝试返回
std::forward_list<Transation*>

编译没有问题:

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));
}
© www.soinside.com 2019 - 2024. All rights reserved.