我怎样才能让 gmock 对象返回一个固定的 std::forward_list

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

提问人:Muc 提问时间:9/8/2023 更新时间:9/8/2023 访问量:30

问:

我正在尝试编写此测试:

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

我不明白问题出在哪里。我试图更改 的类型,使用动态对象,甚至创建了自己的MATCHER_P但我无法弄清楚这一点。transactionList

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

谢谢!

C++ 标准 GoogleMock

评论

0赞 pptaszni 9/8/2023
阅读 meta.stackoverflow.com/questions/366988/what-does-mcve-mean,这个问题绝对可以深入到一些原始类型和 1 个非常简单的模拟类。 对 gmock 没有任何特殊行为。std::forward_list

答:

1赞 pptaszni 9/8/2023 #1

它应该是一个评论,但字符有点太多了。

没有可行的转换

forward_list<Transaction *, allocator<Transaction *>>

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

所以,我有 90% (因为您没有在问题中包含相关信息)确定您有错别字,因为您的模拟返回并且您尝试返回.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));
}

评论

1赞 Muc 9/9/2023
嘿,谢谢你的帮助,如果我错过了一些重要信息,我很抱歉。正如我所提到的,我不是一个重度C++用户,所以可能我不知道什么样的信息是相关的。无论如何,我解决了我的问题,再次感谢!